DevExtreme Angular - Value Formatting

You can change value formatting in DevExtreme applications in two ways:

  • Set a UI component's format property to display formatted values in editors, grids, and other components.
  • Use the localization API to format and parse values programmatically in code.

Format patterns are locale-independent initially. To apply locale-aware formatting rules, use Intl or Globalize.

View Demo

Using Intl

Intl is the ECMAScript Internationalization API. DevExtreme supports Intl for locale-aware number, date, and currency formatting out of the box.

Configure locale and message dictionaries in the UI Text Localization section. This topic focuses on Intl-based value formatting.

View Demo

Use defaultCurrency to set a global currency. Use format.currency in an individual UI component to override that value.

app.component.ts
app.component.html
  • import { Component } from '@angular/core';
  • import { DxDataGridModule } from 'devextreme-angular';
  • import config from 'devextreme/core/config';
  •  
  • @Component({
  • selector: 'app-root',
  • templateUrl: './app.component.html',
  • styleUrls: ['./app.component.css'],
  • standalone: true,
  • imports: [DxDataGridModule]
  • })
  • export class AppComponent {
  • constructor() {
  • config({ defaultCurrency: 'EUR' });
  • }
  • }
  • <dx-data-grid ... >
  • <dxi-data-grid-column dataField="SaleAmount">
  • <dxo-data-grid-format
  • type="currency"
  • currency="USD">
  • </dxo-data-grid-format>
  • </dxi-data-grid-column>
  • </dx-data-grid>

You can apply Intl option objects directly to the format property. Refer to Intl Formats for details and additional examples.

Using Globalize

IMPORTANT

The Globalize package is outdated and potentially unsafe.

Use Globalize at your own risk. We recommend switching to Intl for a more secure solution.

NOTE
We recommend against using Globalize if you apply CSP rules. Use Intl instead.

The following libraries and data are required to activate Globalize in your project:

  • Globalize library
  • CLDR library
  • CLDR data

Install devextreme-cldr-data and globalize packages:

Code
  • npm install --save-dev devextreme-cldr-data globalize

Register Globalize in your project as described in the following help topic: Globalize Registration.

TypeScript
  • import "devextreme/localization/globalize/number";
  • import "devextreme/localization/globalize/date";
  • import "devextreme/localization/globalize/currency";
  • import "devextreme/localization/globalize/message";
  •  
  • import deMessages from "devextreme/localization/messages/de.json";
  • import supplemental from "devextreme-cldr-data/supplemental.json";
  • import deCldrData from "devextreme-cldr-data/de.json";
  • import Globalize from "globalize";
  •  
  • export class AppComponent {
  • constructor() {
  • Globalize.load(supplemental, deCldrData);
  • Globalize.loadMessages(deMessages);
  • Globalize.locale(navigator.language);
  • }
  • }

Apply the format.type property to format and localize strings, numbers, dates, and currencies automatically according to a locale.

You can also format values using structures accepted by numberFormatter, currencyFormatter, and dateFormatter, for example:

app.component.html
app.component.ts
  • <dx-data-grid ... >
  • <dxi-data-grid-column
  • dataField="OrderDate"
  • [format]="{ skeleton: 'yMMMd' }">
  • </dxi-data-grid-column>
  • <dxi-data-grid-column
  • dataField="SaleAmount"
  • [format]="{ currency: 'EUR', maximumFractionDigits: 2 }">
  • </dxi-data-grid-column>
  • </dx-data-grid>
  • import { Component } from '@angular/core';
  • import { DxDataGridModule } from 'devextreme-angular';
  • // ...
  • // import dictionaries and localization modules here
  •  
  • @Component({
  • selector: 'app-root',
  • templateUrl: './app.component.html',
  • styleUrls: ['./app.component.css'],
  • standalone: true,
  • imports: [DxDataGridModule]
  • })
  • export class AppComponent {
  • // ...
  • }

Format UI Component Values

Predefined Formats

Predefined formats are string literals for formatting numbers and dates. See the format.type description for a full list.

Set the format UI component property to apply a predefined format. In the following examples, this property formats the SaleAmount column value in the DataGrid. The precision value of 2 displays two digits after the decimal separator.

app.component.html
app.component.ts
  • <dx-data-grid [dataSource]="orders" keyExpr="ID">
  • <dxi-data-grid-column dataField="SaleAmount">
  • <dxo-data-grid-format
  • type="fixedPoint"
  • [precision]="2">
  • </dxo-data-grid-format>
  • </dxi-data-grid-column>
  • </dx-data-grid>
  • import { Component } from '@angular/core';
  • import { DxDataGridModule } from 'devextreme-angular';
  •  
  • type Order = {
  • ID: number;
  • SaleAmount: number;
  • };
  •  
  • @Component({
  • selector: 'app-root',
  • templateUrl: './app.component.html',
  • styleUrls: ['./app.component.css'],
  • standalone: true,
  • imports: [DxDataGridModule]
  • })
  • export class AppComponent {
  • orders: Order[] = [{
  • ID: 1,
  • SaleAmount: 1234.567
  • }];
  • }

The previous example assigns an object to the format property that in turn allows you to specify the precision value. You can assign a string literal to this format property if additional options are not required.

View Demo

Intl Formats

Intl is the ECMAScript Internationalization API. DevExtreme supports Intl value formatting out of the box.

The UI component format property is compatible with options objects accepted by Intl.NumberFormat and Intl.DateTimeFormat. To apply an Intl format, assign these option fields to the format property:

app.component.html
app.component.ts
  • <dx-data-grid [dataSource]="orders" keyExpr="ID">
  • <dxi-data-grid-column
  • dataField="OrderDate"
  • [format]="orderDateFormat">
  • </dxi-data-grid-column>
  • <dxi-data-grid-column
  • dataField="SaleAmount"
  • [format]="saleAmountFormat">
  • </dxi-data-grid-column>
  • </dx-data-grid>
  • import { Component } from '@angular/core';
  • import { DxDataGridModule } from 'devextreme-angular';
  •  
  • type Order = {
  • ID: number;
  • OrderDate: Date;
  • SaleAmount: number;
  • };
  •  
  • @Component({
  • selector: 'app-root',
  • templateUrl: './app.component.html',
  • styleUrls: ['./app.component.css'],
  • standalone: true,
  • imports: [DxDataGridModule]
  • })
  • export class AppComponent {
  • orders: Order[] = [{
  • ID: 1,
  • OrderDate: new Date(2021, 6, 15),
  • SaleAmount: 1234.567
  • }];
  •  
  • orderDateFormat = { year: '2-digit', month: 'narrow', day: '2-digit' };
  • saleAmountFormat = { style: 'currency', currency: 'EUR', useGrouping: true, minimumSignificantDigits: 3 };
  • }

Custom Format String

Use Unicode Locale Data Markup Language (LDML) patterns to specify a custom format string. An LDML pattern consists of wildcard and literal characters. The format property supports the following wildcard characters:

Numeric Formats

Format characterDescription
0A digit. Displays '0' if the formatted number does not have a digit in that position.
#Any number of leading digits, a single digit, or nothing. If this character goes first in the format string, it can match multiple leading digits (before the decimal point). Subsequent characters match a single digit. If the formatted number does not have a digit in the corresponding position, it displays nothing.
For example, if you format "123.45" with "#0.#", the result is "123.5".
.A decimal separator.
Actual character depends on locale.
,A group separator.
Actual character depends on locale.
%The percent sign. Multiplies the input value by 100.
;Separates positive and negative format patterns.
For example, the "#0.##;(#0.##)" format displays a positive number according to the pattern before the semicolon (";"), and a negative number according to the pattern after the semicolon (";").
If you do not use this character , nor the additional pattern, negative numbers display a minus ("-") prefix.
Escape charactersYou can display the special characters above as literals if you enclose them in single quotation marks.
For example, '%'.
Other charactersYou can add any literal characters to the beginning or end of the format string.

The examples below demonstrate how # and 0 behave in fractional numbers:

JavaScript
  • const number = 1234.567;
  •  
  • // Leave the first digit before the decimal point and round up the decimal
  • format: "0.0" // 4.6
  •  
  • const smallNumber = 0.1234;
  •  
  • // Display nothing in place of a digit
  • format: "#.#" // .1
  •  
  • const largeNumber = 123456.789;
  •  
  • // Add a group separator
  • format: ",##0.###" // 123,456.789

The examples below demonstrate different options used to apply percentage formatting to decimals. Use caution if your format string starts with 0 because the formatted number may lose leading digits.

JavaScript
  • const smallNumber = 0.01234;
  •  
  • // Represent as a percentage and limit to two decimal digits
  • format: "#0.##%" // 1.23%
  •  
  • // Add a percent sign and limit to two decimal digits
  • format: "#0.##'%'" // 0.01%

Date-Time Formats

Format characterDescription
yA calendar year.
QA quarter number or name.
Available combinations with example: "Q" - "2", "QQ" - "02", "QQQ" - "Q2" and "QQQQ" - "2nd quarter".
MA month number or name.
Available combinations with example: "M" - "9", "MM" - "09", "MMM" - "Sep", "MMMM" - "September", "MMMMM" - "S".
dA month day.
EA week day name.
Available combinations with example: "E", "EE" or "EEE" - "Tue", "EEEE" - "Tuesday", "EEEEE" - "T".
aA day period (am or pm).
hAn hour. From 1 to 12.
HAn hour. From 0 to 23.
mA minute.
sA second.
SA fractional second.
'' (two single quotes)Literal text. Text enclosed in two single quotes is shown as-is.
JavaScript
  • const date = new Date(2021, 6, 15, 20, 45, 34);
  •  
  • format: "MM/dd/yyyy" // 07/15/2021
  • format: "MM/dd/yy" // 07/15/21
  • format: "dd.MM.yyyy" // 15.07.2021
  • format: "MMMM dd, yyyy" // July 15, 2021
  • format: "EEEE, MMMM dd" // Thursday, July 15
  • format: "HH:mm:ss" // 20:45:34
  • format: "hh:mm a" // 08:45 PM
  • format: "MMMM dd, yyyy HH:mm:ss" // July 15, 2021 20:45:34
NOTE
Use the Globalize library in your application to use additional numeric or date-time format characters.

Custom Function

A custom function is useful when advanced formatting is required. The value to be formatted is passed to the function as an argument. In the following example, a custom function formats a DataGrid column value as an absolute value and a percentage of the maximum sale amount.

app.component.html
app.component.ts
  • <dx-data-grid [dataSource]="orders" keyExpr="ID">
  • <dxi-data-grid-column
  • dataField="SaleAmount"
  • [format]="formatSaleAmount">
  • </dxi-data-grid-column>
  • </dx-data-grid>
  • import { Component } from '@angular/core';
  • import { DxDataGridModule } from 'devextreme-angular';
  •  
  • type Order = {
  • ID: number;
  • SaleAmount: number;
  • };
  •  
  • @Component({
  • selector: 'app-root',
  • templateUrl: './app.component.html',
  • styleUrls: ['./app.component.css'],
  • standalone: true,
  • imports: [DxDataGridModule]
  • })
  • export class AppComponent {
  • orders: Order[] = [{
  • ID: 1,
  • SaleAmount: 6
  • }, {
  • ID: 2,
  • SaleAmount: 8.5
  • }, {
  • ID: 3,
  • SaleAmount: 10
  • }];
  •  
  • maxSaleAmount = Math.max(...this.orders.map((item) => item.SaleAmount));
  •  
  • formatSaleAmount = (value: number): string => {
  • return `${value} | ${((value / this.maxSaleAmount) * 100).toFixed(1)}%`;
  • };
  • }

Use Different Locales for Value Formats and UI Text

DevExtreme libraries use the current application locale to display UI text and to format values. You can use a different locale to format date, time, and number values. To do that, specify Format.locale in the following GlobalConfig properties:

The following code snippet displays German UI text and specifies the "en-CA" locale in dateFormat and the "en-US" locale in numberFormat:

app.component.ts
  • import deMessages from 'devextreme/localization/messages/de.json';
  • import { loadMessages, locale } from 'devextreme/localization';
  • import config from 'devextreme/core/config';
  •  
  • loadMessages(deMessages);
  • locale('de');
  •  
  • config({
  • dateFormat: {
  • default: {
  • locale: 'en-CA',
  • },
  • },
  • numberFormat: {
  • default: {
  • locale: 'en-US',
  • },
  • },
  • });

Components that use these global formats display values as follows:

Raw ValueFormatted Value
January 20, 20272027-01-20
1234.56781,234.568
NOTE
dateFormat, timeFormat, dateTimeFormat, and numberFormat only configure default formats. locale values specified in these properties do not affect formats defined in components.

To apply a specific locale to a predefined date/time format (format.type), override this format in dateTimeFormatPresets with a format object that specifies locale. The following code snippet displays "longDate" values in the "en-CA" locale:

app.component.ts
  • import config from 'devextreme/core/config';
  •  
  • config({
  • dateTimeFormatPresets: {
  • longDate: {
  • default: {
  • type: 'longDate',
  • locale: 'en-CA',
  • },
  • },
  • },
  • });

Define locale in a component format property to format component values in a specific locale. The following code snippet configures displayFormat.locale in a DevExtreme DateBox:

app.component.html
app.component.ts
  • <dx-date-box [value]="date">
  • <dxo-date-box-display-format locale="en-CA"></dxo-date-box-display-format>
  • </dx-date-box>
  • import { Component } from '@angular/core';
  • import { DxDateBoxModule } from 'devextreme-angular';
  •  
  • @Component({
  • // ...
  • imports: [DxDateBoxModule]
  • })
  • export class AppComponent {
  • date: Date = new Date(2027, 1, 20);
  • }

Format and Parse Dates and Numbers

DevExtreme includes an API that formats and parses dates and numbers. The following example demonstrates date and number formatting and parsing.

The formatDate() method applies a predefined format to a date. You can also use a custom function or format string. The output is a string. Use parseDate() to convert the string back to a Date.

Use formatNumber() and parseNumber() to format and parse numbers and currency values.

TypeScript
HTML
  • import { formatDate, formatNumber, parseDate, parseNumber } from "devextreme/localization";
  • // ...
  • export class AppComponent {
  • _date: Date = new Date();
  • _amount: number = 1234.56;
  • get date() {
  • return formatDate(this._date, "shortDate");
  • }
  • set date(value) {
  • this._date = parseDate(value, "shortDate");
  • }
  • get amount() {
  • return formatNumber(this._amount, "currency");
  • }
  • set amount(value) {
  • this._amount = parseNumber(value, "currency");
  • }
  • }
  • <input
  • [value]="date"
  • (change)="date = $event.target.value"
  • />
  • <input
  • [value]="amount"
  • (change)="amount = $event.target.value"
  • />