Intl.NumberFormat.prototype.format()

The Intl.NumberFormat.prototype.format() method formats a number according to the locale and formatting options of this Intl.NumberFormat object.

Try it

Syntax

format(number)

Parameters

number

A Number or BigInt to format.

Description

The format getter function formats a number into a string according to the locale and formatting options of this Intl.NumberFormat object.

Note: Numbers in JavaScript suffer from loss of precision if they are too big or too small, making the text representation inaccurate. If you are formatting an integer larger than Number.MAX_SAFE_INTEGER, prefer using a BigInt instead.

new Intl.NumberFormat('en-US').format(1234567891234567891) // 1,234,567,891,234,568,000
new Intl.NumberFormat('en-US').format(1234567891234567891n) // 1,234,567,891,234,567,891

Examples

Using format

Use the format getter function for formatting a single currency value, here for Russia:

const options = { style: 'currency', currency: 'RUB' };
const numberFormat = new Intl.NumberFormat('ru-RU', options);
console.log(numberFormat.format(654321.987));
// → "654 321,99 руб."

Using format with map

Use the format getter function for formatting all numbers in an array. Note that the function is bound to the Intl.NumberFormat from which it was obtained, so it can be passed directly to Array.prototype.map. This is considered a historical artefact, as part of a convention which is no longer followed for new features, but is preserved to maintain compatibility with existing programs.

const a = [123456.789, 987654.321, 456789.123];
const numberFormat = new Intl.NumberFormat('es-ES');
const formatted = a.map(n => numberFormat.format(n));
console.log(formatted.join('; '));
// → "123.456,789; 987.654,321; 456.789,123"

Specifications

Specification
ECMAScript Internationalization API Specification
# sec-intl.numberformat.prototype.format

Browser compatibility

BCD tables only load in the browser

See also