Intl.NumberFormat
Formats numbers, currencies, percents and units by language rules.
Available since: ES2015 (Intl)
Syntax
new Intl.NumberFormat(locales, options)Parameters
| Parameter | Type | Description |
|---|---|---|
| locales | string | Optional. The language and region, for example en-US or de-DE. |
| options | object | Optional. style (decimal, currency, percent, unit), currency, unit, notation, minimumFractionDigits, maximumFractionDigits. |
Return value
Intl.NumberFormatAn object with a format() method.
Examples
Prices in different currencies:
const usd = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
const eur = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' });
console.log(usd.format(1234.5));
console.log(eur.format(1234.5));Output:
$1,234.50
1.234,50 €Compact notation and units:
console.log(new Intl.NumberFormat('en-US', { notation: 'compact' }).format(1250000));
console.log(new Intl.NumberFormat('en-US', { style: 'unit', unit: 'kilometer-per-hour' }).format(90));Output:
1.3M
90 km/hThe number of decimal places:
const formatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 1 });
console.log(formatter.format(3.14159));
console.log(formatter.format(2));Output:
3.1
2Notes
In German and Russian formats the group separator is a space or a dot and the decimal separator is a comma. Such strings cannot be turned back into numbers with Number(): format only for display.
See also
- Number.prototype.toLocaleString — Formats a number by language rules: digit grouping, currency, percent.
- Number.prototype.toFixed — Formats a number with a given number of decimal places.
- Intl.PluralRules — Picks the word form for a number: 1 file, 2 files.
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.