Intl.NumberFormat.prototype.formatToParts()

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.

Syntax

Intl.NumberFormat.prototype.formatToParts(number)

Parameters

number Optional
The number to format.

Return value

An Array of objects containing the formatted number in parts.

Description

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:

currency
The currency string, such as the symbols "$" and "€" or the name "Dollar", "Euro" depending on how currencyDisplay is specified.
decimal
The decimal separator string (".").
fraction
The fraction number.
group
The group separator string (",").
infinity
The Infinity string ("∞").
integer
The integer number.
literal
Any literal strings or whitespace in the formatted number.
minusSign
The minus sign string ("-").
nan
The NaN string ("NaN").
plusSign
The plus sign string ("+").
percentSign
The percent sign string ("%").

Examples

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>"

Polyfill

A polyfill for this feature is available in the proposal repository.

Specifications

Specification Status Comment
ECMAScript Internationalization API 4.0 (ECMA-402)
The definition of 'Intl.NumberFormat.prototype.formatToParts' in that specification.
Draft Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Basic support
Experimental
Chrome Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Need to set the flag on the commandline via --js-flags
Disabled From version 63: this feature is behind the harmony-number-format-to-parts runtime flag.
Edge Full support YesFirefox Full support 58IE ? Opera ? Safari ? WebView Android No support NoChrome Android Full support 63
Notes Disabled
Full support 63
Notes Disabled
Notes Need to set the flag on the commandline via --js-flags
Disabled From version 63: this feature is behind the harmony-number-format-to-parts runtime flag.
Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android No support Nonodejs ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also

Document Tags and Contributors

Contributors to this page: alexrussell, fscholz
Last updated by: alexrussell,