Number.prototype.toLocaleString
Formats a number by language rules: digit grouping, currency, percent.
Available since: ES3
Syntax
number.toLocaleString(locales, options)Parameters
| Parameter | Type | Description |
|---|---|---|
| locales | string | Optional. The language and region, for example en-US or de-DE. |
| options | object | Optional. Settings: style, currency, minimumFractionDigits and others. |
Return value
stringThe number formatted by the rules of the language.
Examples
Digit grouping in different languages:
const n = 1234567.891;
console.log(n.toLocaleString('en-US'));
console.log(n.toLocaleString('de-DE'));Output:
1,234,567.891
1.234.567,891Currency and percent:
console.log((1999.5).toLocaleString('en-US', { style: 'currency', currency: 'USD' }));
console.log((0.256).toLocaleString('en-US', { style: 'percent' }));Output:
$1,999.50
26%Notes
Without locales the user's browser language is used, so the result may differ between computers. To format many numbers it is faster to create one Intl.NumberFormat.
See also
- Intl.NumberFormat — Formats numbers, currencies, percents and units by language rules.
- Number.prototype.toFixed — Formats a number with a given number of decimal places.
- Date.prototype.toLocaleString — Formats a date together with the time by language rules.
Related recipes
In the tutorial
- Formatting with Intl — Locale-aware formatting of numbers, money, dates and plural words in JavaScript: toLocaleString, Intl.NumberFormat, Intl.DateTimeFormat and Intl.PluralRules.