JS: Number .prototype .toLocaleString

By Xah Lee. Date: . Last updated: .

Number.prototype.toLocaleString

Number.prototype.toLocaleString(locales, options)

Return a string that represents this number, formatted according to the conventions of the host environment's current locale.

such as digits group separator, decimal point marker, currency symbols, percentage, etc.

Parameters

locales

A string or array of BCP 47 language tags or an array of it. If omitted, it uses the user's browser/system default locale.

e.g. "en-US" or ["zh-Hans", "en-US"]

if given array, it will use the first supported locale.

options

An object to customize formatting (style, currency, minimum/maximum digits, etc.).

example: { style: "currency", currency: "GBP" }

Example. number separator

// default. use browser locale
console.log(
 (262457.086).toLocaleString(),
);
// 262,457.086

// s------------------------------

console.log(
 (262457.086).toLocaleString("en-US"),
);
// 262,457.086

// germany
console.log(
 (262457.086).toLocaleString("de-DE"),
);
// 262.457,086

// france
console.log(
 (262457.086).toLocaleString("fr-FR"),
);
// 262 457,086

// Arabic in Egypt
console.log(
 (262457.086).toLocaleString("ar-EG"),
);
// ٢٦٢٬٤٥٧٫٠٨٦

Example. currency formatting

console.log(
 (4795.99).toLocaleString("en-US", {
  style: "currency",
  currency: "USD",
 }),
);
// $4,795.99

console.log(
 (4795.99).toLocaleString("en-US", {
  style: "currency",
  currency: "USD",
  currencyDisplay: "code",
 }),
);
// USD 4,795.99

// s------------------------------

console.log(
 (4795.99).toLocaleString("de-DE", {
  style: "currency",
  currency: "EUR",
 }),
);
// 4.795,99 €

Example. percentage

console.log(
 (0.1089).toLocaleString("en-US", {
  style: "percent",
 }),
);
// 11%

console.log(
 (0.1089).toLocaleString("en-US", {
  style: "percent",
  minimumFractionDigits: 2,
  maximumFractionDigits: 2,
 }),
);
// 10.89%

Example. metric prefix

console.log(
 (5_601).toLocaleString(
  "en-US",
  { notation: "compact" },
 ),
);
// 5.6K

console.log(
 (365_601).toLocaleString(
  "en-US",
  { notation: "compact" },
 ),
);
// 366K

console.log(
 (3_365_601).toLocaleString(
  "en-US",
  { notation: "compact" },
 ),
);
// 3.4M

console.log(
 (9_403_365_601).toLocaleString(
  "en-US",
  { notation: "compact" },
 ),
);
// 9.4B

// s------------------------------

console.log(
 (9_403_365_601).toLocaleString("en-US", {
  notation: "compact",
  compactDisplay: "long",
 }),
);
// 9.4 billion

Example. fixed decimal places

console.log(
 (327.2988).toLocaleString("en-US", {
  minimumFractionDigits: 3,
  maximumFractionDigits: 3,
 }),
);
// 327.299

console.log(
 (327.2988).toLocaleString("en-US", {
  minimumFractionDigits: 0,
  maximumFractionDigits: 0,
 }),
);
// 327

Example. unit formatting

console.log(
 (42.5).toLocaleString("en-US", {
  style: "unit",
  unit: "kilometer-per-hour",
  unitDisplay: "long",
 }),
);
// 42.5 kilometers per hour

The options object

style
"decimal" (default), "currency", "percent", "unit" ◆ Main formatting style
currency
ISO currency code (e.g. "USD", "EUR", "JPY") ◆ Required when style: "currency"
currencyDisplay
"symbol" (default), "narrowSymbol", "code", "name" ◆ How currency is shown
minimumFractionDigits
0 to 100 ◆ Minimum decimal places
maximumFractionDigits
0 to 100 ◆ Maximum decimal places
minimumIntegerDigits
1 to 21 ◆ Minimum digits before decimal
useGrouping
true/false ◆ Show thousands separators
notation
"standard", "scientific", "engineering", "compact" ◆ Compact notation (1.2K, 1.2M, etc.)
compactDisplay
"short", "long" ◆ Used with notation: "compact"

Intl.NumberFormat

Number.prototype.toLocaleString is a wrapper around Intl.NumberFormat.

this

Number.toLocaleString(locales, options)

is same as

new Intl.NumberFormat(locales, options).format(number)

console.log(
 new Intl.NumberFormat("en-US").format(2418.5335),
);
// 2,418.534

// is equivalent to

console.log(
 (2418.5335).toLocaleString("en-US"),
);
// 2,418.534

For repeated call, hundreds or thousand of times in a loop, it is more efficient to use Intl.NumberFormat(locales, options) to create a function f, then use f.format(num).

const xformat = new Intl.NumberFormat(
 "en-US",
 {
  style: "currency",
  currency: "USD",
 },
);

console.log(
 xformat.format(1234.56),
); // "$1,234.56"