The Intl.NumberFormat.prototype.formatToParts()
method allows locale-aware formatting of strings produced by NumberFormat
formatters.
The Intl.NumberFormat.prototype.formatToParts()
method allows locale-aware formatting of strings produced by NumberFormat
formatters.
formatToParts() formatToParts(number)
An Array
of objects containing the formatted number in parts.
The formatToParts()
method is useful for custom formatting of number strings. It returns an Array
of objects containing the locale-specific tokens from which it possible to build custom strings while preserving the locale-specific parts. The structure the formatToParts()
method returns, looks like this:
[ { type: "integer", value: "3" }, { type: "group", value: "." }, { type: "integer", value: "500" } ]
Possible types are the following:
The currency string, such as the symbols "$" and "€" or the name "Dollar", "Euro" depending on how currencyDisplay
is specified.
The decimal separator string (".").
The fraction number.
The group separator string (",").
The Infinity
string ("∞").
The integer number.
Any literal strings or whitespace in the formatted number.
The minus sign string ("-").
The NaN
string ("NaN").
The plus sign string ("+").
The percent sign string ("%").
The unit string, such as the "l" or "litres", depending on how unitDisplay
is specified.
NumberFormat
outputs localized, opaque strings that cannot be manipulated directly:
const number = 3500; const formatter = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }); formatter.format(number); // "3.500,00 €"
However, in many User Interfaces there is a desire to customize the formatting of this string. The formatToParts
method enables locale-aware formatting of strings produced by NumberFormat
formatters by providing you the string in parts:
formatter.formatToParts(number); // return value: [ { type: "integer", value: "3" }, { type: "group", value: "." }, { type: "integer", value: "500" }, { type: "decimal", value: "," }, { type: "fraction", value: "00" }, { type: "literal", value: " " }, { type: "currency", value: "€" } ]
Now the information is available separately and it can be formatted and concatenated again in a customized way. For example by using Array.prototype.map()
, arrow functions, a switch statement, template literals, and Array.prototype.reduce()
.
const numberString = formatter.formatToParts(number).map(({type, value}) => { switch (type) { case 'currency': return `<strong>${value}</strong>`; default : return value; } }).reduce((string, part) => string + part);
This will make the currency bold, when using the formatToParts()
method.
console.log(numberString); // "3.500,00 <strong>€</strong>"
Specification |
---|
ECMAScript Internationalization API Specification # sec-intl.numberformat.prototype.formattoparts |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
formatToParts |
64 |
12 |
58 |
No |
51 |
13 |
64 |
64 |
58 |
47 |
13 |
9.0 |
1.8 |
10.0.0
Before version 13.0.0, only the locale data for
en-US is available by default. See the NumberFormat() constructor for more details. |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/formatToParts