This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The Intl.Numberformat.prototype.formatToParts()
method allows locale-aware formatting of strings produced by NumberTimeFormat
formatters.
Intl.NumberFormat.prototype.formatToParts(number)
number
Optional
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:
currencyDisplay
is specified.Infinity
string ("∞").NaN
string ("NaN").NumberFormat
outputs localized, opaque strings that cannot be manipulated directly:
var number = 3500; var 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()
.
var 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>"
A polyfill for this feature is available in the proposal repository.
Specification | Status | Comment |
---|---|---|
ECMAScript Internationalization API 4.0 (ECMA-402) The definition of 'Intl.NumberFormat.prototype.formatToParts' in that specification. | Draft | Initial definition |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 631 2 | ? | 58 | ? | ? | ? |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | No | 631 2 | ? | ? | ? | ? | ? |
1. Need to set the flag on the commandline via --js-flags
2. From version 63: this feature is behind the harmony-number-format-to-parts
runtime flag.
Intl.NumberFormat
Intl.NumberFormat.prototype.format
Intl.DateTimeFormat.prototype.formatToParts()
© 2005–2018 Mozilla Developer Network and individual 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/NumberFormat/formatToParts