Number Formatting in Blazor Pivot Table
15 Aug 202610 minutes to read
The Pivot Table component lets you display numeric values in standard number, currency, percentage, or custom formats to match the reporting needs of your application.
Supported format types
The Pivot Table component supports the following display formats for numeric values:
- Number - Standard numeric formatting with optional grouping separators and configurable decimal places.
- Currency - Formats currency values with appropriate symbols, optional grouping separators, and customizable decimal places.
- Percentage - Values displayed as percentages with the % symbol.
- Custom - User-defined formatting patterns for specific display requirements.
Defining number format settings
To configure number formats for numeric values, use the FormatSettings property inside PivotViewDataSourceSettings.
The following properties are available inside each FormatSettings entry:
Essential formatting properties
| Property | Type | Description |
|---|---|---|
| Name | String |
The field name to which the formatting should be applied. |
| Format | String |
The format pattern for the field. |
Format type codes
Use these standard format codes as the value of the Format property. You can also append a digit to set the number of decimal places (e.g., N2 for two decimal places).
-
N - Numeric formatting (e.g.,
Nproduces1,234.56;N2produces1,234.56). -
C - Currency formatting (e.g.,
C0produces$1,234; the symbol is taken from thecurrencyproperty). -
P - Percentage formatting (e.g.,
P1produces12.3%for the value0.1234).
NOTE
When no format is specified, the component applies numeric formatting by default.
Additional formatting options
-
UseGrouping (
boolean, defaulttrue): Controls the display of grouping separators. When true (default), values display with separators (for example$100,000,000); when false, they display without separators (for example,$100000000). -
Currency (
String): The currency code to be considered for currency formatting (for example,USD,EUR,GBP).
@using Syncfusion.Blazor.PivotView
<SfPivotView TValue="ProductDetails">
<PivotViewDataSourceSettings DataSource="@data">
<PivotViewColumns>
<PivotViewColumn Name="Year"></PivotViewColumn>
<PivotViewColumn Name="Quarter"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Country"></PivotViewRow>
<PivotViewRow Name="Products"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Sold" Caption="Units Sold"></PivotViewValue>
<PivotViewValue Name="Amount" Caption="Sold Amount"></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C2" UseGrouping="false" Currency='EUR'></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
</SfPivotView>
@code{
public List<ProductDetails> data { get; set; }
protected override void OnInitialized()
{
this.data = ProductDetails.GetProductData().ToList();
//Bind the data source collection here. Refer "Assigning sample data to the pivot table" section in getting started for more details.
}
}
You can also format the values at runtime using the formatting dialog. This option can be enabled by setting the AllowNumberFormatting property to true. The same has been discussed in some of the upcoming topics.
NOTE
Important: To use the runtime formatting dialog (and the toolbar option), include the
NumberFormattingmodule in the Pivot Table:
Custom format
Custom format lets you display numbers in your preferred pattern by setting the Format property within the FormatSettings. You can use one or more format specifiers (shown in the table below) to control how values appear in the Pivot Table. The Input column shows the format value applied to the value 123 (except where noted).
| Specifier | Description | Input | Format Output |
|---|---|---|---|
| 0 | Replaces the zero with the corresponding digit if one is present. Otherwise, zero appears in the result string. | { format: '0000' } |
'0123' |
| # | Replaces the # symbol with the corresponding digit if one is present. Otherwise, no digit appears in the result string. |
{ format: '####' } |
'1234' |
| . | Denotes the number of digits permitted after the decimal point. | { format: '###0.##0#' } |
'546321.000' (value 546321) |
| % | Percent specifier; multiplies the value by 100 and appends the % symbol. |
{ format: '0000 %' } |
'0100 %' (value 1) |
| $ | Denotes currency formatting based on the global currency code specified in currency. |
{ format: '$ ###.00' } |
'$ 13.00' (value 13) |
| ; | Denotes separate formats for positive, negative, and zero values. | { format: '###.##;(###.00);-0' } |
'(120.00)' (value -120) |
'String' |
Characters enclosed in single quotes are included literally in the result string. | { format: "####.00 '@'" } |
'123.00 @' (value 123) |
NOTE
When you define a custom format, certain properties such as UseGrouping and Currency in the format settings will be ignored.
@using Syncfusion.Blazor.PivotView
<SfPivotView TValue="ProductDetails">
<PivotViewDataSourceSettings DataSource="@data">
<PivotViewColumns>
<PivotViewColumn Name="Year"></PivotViewColumn>
<PivotViewColumn Name="Quarter"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Country"></PivotViewRow>
<PivotViewRow Name="Products"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Sold" Caption="Units Sold"></PivotViewValue>
<PivotViewValue Name="Amount" Caption="Sold Amount"></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C"></PivotViewFormatSetting>
<PivotViewFormatSetting Name="Sold" Format="####.00 'Nos'"></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
</SfPivotView>
@code{
public List<ProductDetails> data { get; set; }
protected override void OnInitialized()
{
this.data = ProductDetails.GetProductData().ToList();
//Bind the data source collection here. Refer "Assigning sample data to the pivot table" section in getting started for more details.
}
}
Toolbar
Number formatting can be applied at runtime through the built-in dialog, accessible from the toolbar. To enable this, set both the AllowNumberFormatting and ShowToolbar properties to true, include the NumberFormatting module, and add the 'NumberFormatting' option to the Toolbar property. The toolbar then displays the Number Formatting icon. Clicking this icon opens the dialog, where you can specify number formats for value fields directly within the Pivot Table.
@using Syncfusion.Blazor.PivotView
<SfPivotView @ref="pivot" TValue="ProductDetails" ShowToolbar="true" Toolbar="@toolbar" AllowNumberFormatting="true" Height="300" Width="800">
<PivotViewDisplayOption Primary=Primary.Table View=View.Both></PivotViewDisplayOption>
<PivotViewDataSourceSettings DataSource="@data">
<PivotViewColumns>
<PivotViewColumn Name="Year"></PivotViewColumn>
<PivotViewColumn Name="Quarter"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="Country"></PivotViewRow>
<PivotViewRow Name="Products"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="Sold" Caption="Units Sold"></PivotViewValue>
<PivotViewValue Name="Amount" Caption="Sold Amount"></PivotViewValue>
</PivotViewValues>
<PivotViewFormatSettings>
<PivotViewFormatSetting Name="Amount" Format="C"></PivotViewFormatSetting>
</PivotViewFormatSettings>
</PivotViewDataSourceSettings>
<PivotViewEvents TValue="ProductDetails"></PivotViewEvents>
<PivotViewGridSettings ColumnWidth="140"></PivotViewGridSettings>
</SfPivotView>
<style>
.e-pivotview {
min-height: 300px;
width: 722px;
}
</style>
@code{
SfPivotView<ProductDetails> pivot;
public List<ToolbarItems> toolbar = new List<ToolbarItems> {
ToolbarItems.NumberFormatting
};
public List<ProductDetails> data { get; set; }
protected override void OnInitialized()
{
this.data = ProductDetails.GetProductData().ToList();
//Bind the data source collection here. Refer "Assigning sample data to the pivot table" section in getting started for more details.
}
}
NOTE
You can refer to the Blazor Pivot Table feature tour page for its groundbreaking feature representations. You can also explore the Blazor Pivot Table example to know how to render and configure the pivot table.