The format()
method formats the number to a localized string.
The formatToParts()
method outputs an array with each part of the number separated by parts.
This is useful for determining the properties of a locale, which can be useful for parsing numbers. See the parsing guide.
Changing the numbering system
You can change the numbering system in your locale by adding -u-nu-{code}
.
One common example is the Chinese locale of zh
that uses latn
as the default numbering system:
But hanidec
is also very common in this locale, and would look like this:
Another way to change the numbering system is to specify the numberingSystem
option.
Number of Decimals Places
By default, decimals are rounded to 3 digits:
To override the default number of decimal places, use the minimumFractionDigits
option:
Currency Formatting
To format a number as a currency, you need to set the style and currency options:
Percent Formatting
To format a number as a percent, the number should be between 0 and 1, and the style should be set to percent: