Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Intl.NumberFormat

Formats numbers, currencies, percents and units by language rules.

Available since: ES2015 (Intl)

Syntax

new Intl.NumberFormat(locales, options)

Parameters

ParameterTypeDescription
localesstringOptional. The language and region, for example en-US or de-DE.
optionsobjectOptional. 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/h

The 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
2

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.