Skip to content
jshacks.dev
Fundamentals

JavaScript Number.prototype.toLocaleString Explained

toLocaleString() formats a number for a locale with grouping, currency, percent and unit options. See the default digit limit and how to reuse formatters.

7 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, Vite, TypeScript and npm logos.

(1234567.891).toLocaleString() prints 1,234,567.891 on one machine and 1.234.567,891 on another, with no change in your code. That is the feature, and it is also the reason a test passes on a laptop and fails in a container: when you leave the locale out, the method asks the environment what the user expects. This post covers the arguments, the options you will actually use for currency, percent and units, the default limit of three decimals that surprises people, and the habit that saves the most trouble, which is to always pass the locale. For fixed-decimal output without localization, [the post on toFixed()](/library/javascript-number-prototype-tofixed) is the simpler tool. Examples run on Node.js 24, which ships with full internationalization data, and the options shown here are widely supported.

What toLocaleString() takes and returns

The signature is number.toLocaleString(locales, options). Both arguments are optional. locales is a BCP 47 tag such as "en-US" or "de-DE", or an array of tags in order of preference. options is the same options object that Intl.NumberFormat accepts, and the two produce identical results, because the method creates a formatter and uses it. The return value is always a string.

ArgumentRoleExample
localesLanguage and region rules"en-US", "de-DE", "en-IN"
styleKind of number"decimal", "currency", "percent", "unit"
currencyRequired with the currency style"USD", "EUR", "GBP"
minimumFractionDigitsLowest decimals shown2
maximumFractionDigitsHighest decimals shown0
notationStandard or abbreviated"compact"
useGroupingShow group separatorsfalse
JavaScript
const n = 1234567.891;

console.log(n.toLocaleString("en-US"));
console.log(n.toLocaleString("de-DE"));
console.log(n.toLocaleString("en-IN"));
console.log(n.toLocaleString("en-US", { useGrouping: false }));
console.log(typeof n.toLocaleString("en-US"));
Text
1,234,567.891
1.234.567,891
12,34,567.891
1234567.891
string

German uses a period to group and a comma for the decimal mark, which is the reverse of US English. Indian English groups the last three digits and then pairs of digits. A hand-written formatter that inserts a comma every three digits is correct for only one of these.

The default of three decimals

Without options, the method shows at most three digits after the decimal point and rounds the rest. It does not pad, so integers print without a fraction. That default is the reason 0.123456 appears as 0.123.

JavaScript
console.log((0.123456).toLocaleString("en-US"));
console.log((5).toLocaleString("en-US"));
console.log((5.5).toLocaleString("en-US"));
console.log((1.23456).toLocaleString("en-US"));
console.log((2.5).toLocaleString("en-US", { maximumFractionDigits: 0 }));
console.log((3.5).toLocaleString("en-US", { maximumFractionDigits: 0 }));
console.log((5).toLocaleString("en-US", { minimumFractionDigits: 2 }));
Text
0.123
5
5.5
1.235
3
4
5.00

The fourth line shows the third decimal being rounded up, from 1.23456 to 1.235. Intl rounds ties away from zero by default, so 2.5 becomes 3 and 3.5 becomes 4. Use minimumFractionDigits for a column that must always show cents.

Currency

Currency formatting needs the currency option, because a locale does not imply a currency. The locale controls the layout, and the currency code controls the symbol and the default number of decimals.

JavaScript
const amount = 1234.5;

console.log(amount.toLocaleString("en-US", { style: "currency", currency: "USD" }));
console.log(amount.toLocaleString("en-GB", { style: "currency", currency: "GBP" }));
console.log(amount.toLocaleString("en-US", { style: "currency", currency: "EUR" }));
console.log(amount.toLocaleString("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 0 }));
Text
$1,234.50
£1,234.50
€1,234.50
$1,235

The third line is a US reader looking at euros, so the symbol is € but the layout stays American. Currencies that have no minor unit, such as the Japanese yen, default to zero decimals without any option from you. Two rules we follow: never assume the currency from the locale, and never format an amount that is stored in cents without dividing first, since 123450 formatted as dollars is a very different number.

Percent, compact and units

The percent style multiplies by one hundred, so you pass a fraction. Compact notation shortens large values, and the unit style attaches a measurement.

JavaScript
console.log((0.256).toLocaleString("en-US", { style: "percent" }));
console.log((0.256).toLocaleString("en-US", { style: "percent", minimumFractionDigits: 1 }));
console.log((1234567).toLocaleString("en-US", { notation: "compact" }));
console.log((1500).toLocaleString("en-US", { notation: "compact" }));
console.log((16).toLocaleString("en-US", { style: "unit", unit: "kilometer-per-hour" }));
Text
26%
25.6%
1.2M
1.5K
16 km/h

A common mistake is passing 25.6 to the percent style and receiving 2,560%. The input is a ratio. Unit identifiers come from a fixed list in the specification, so a made-up unit name throws a RangeError.

Special values and BigInt

NaN, the infinities and BigInt values are handled sensibly. A BigInt formats exactly, with no loss at any size, which a double cannot do.

JavaScript
console.log(NaN.toLocaleString("en-US"));
console.log(Infinity.toLocaleString("en-US"));
console.log((12345678901234567890n).toLocaleString("en-US"));
Text
NaN
∞
12,345,678,901,234,567,890

The BigInt keeps every digit. Converting it with Number() first would round it to the nearest double, a point covered in [the post on Number.isSafeInteger()](/library/javascript-number-issafeinteger). Format identifiers and large counts as BigInt or as plain strings.

A mistake we made: relying on the default locale

We formatted totals in a server-rendered page with a bare toLocaleString(). The server ran in a container with a minimal environment, the browser ran with the visitor's language, and the two disagreed. The server wrote 1,234.5 and the browser, after hydration, wanted 1.234,5, which produced a mismatch warning and a visible flash for visitors in some regions.

JavaScript
const total = 1234.5;

const fixed = total.toLocaleString("en-US");
console.log(fixed);

const userLocale = "de-DE";
console.log(total.toLocaleString(userLocale));
console.log(Number.isNaN(Number(total.toLocaleString(userLocale))));
console.log(Number.isNaN(Number(total.toLocaleString("en-US"))));
Text
1,234.5
1.234,5
true
true

The last two lines show a second trap. The formatted text is for people, so converting it back with Number() returns NaN in both locales, because neither a grouping separator nor a decimal comma is valid in a numeric string. Keep the raw number in your data, and format only at the edge. The fix for the mismatch was to choose the locale explicitly from the request, pass the same value on the server and the client, and keep formatting out of anything that is parsed again.

Reusing a formatter

Each call to toLocaleString() with locale and options builds a formatter internally. If you format many numbers with the same settings, create one Intl.NumberFormat and reuse it. The output is identical, and the setup work happens once. We cannot quote a speedup for your data, so measure before you decide it matters.

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

const prices = [4.5, 1299, 0.99];
console.log(prices.map((p) => usd.format(p)));
console.log(usd.format(1234.5) === (1234.5).toLocaleString("en-US", { style: "currency", currency: "USD" }));
console.log(
  usd
    .formatToParts(1234.5)
    .map((p) => `${p.type}=${p.value}`)
    .join(" ")
);
Text
[ '$4.50', '$1,299.00', '$0.99' ]
true
currency=$ integer=1 group=, integer=234 decimal=. fraction=50

formatToParts() splits the result into labeled pieces, which is the way to style the currency symbol or the decimals separately without parsing the string yourself.

Comparing number formatting methods

NeedUse
Locale-aware grouping and symbolstoLocaleString(locale, options)
Many numbers, one configurationIntl.NumberFormat and format()
Fixed decimals, no localizationtoFixed(n)
Fixed significant digitstoPrecision(n)
Parts for custom stylingformatToParts()
JavaScript
const v = 9876.54321;

console.log(v.toFixed(2));
console.log(v.toPrecision(5));
console.log(v.toLocaleString("en-US", { maximumFractionDigits: 2 }));
console.log(v.toLocaleString("de-DE", { maximumFractionDigits: 2 }));
Text
9876.54
9876.5
9,876.54
9.876,54

The first two use a period regardless of the user's region and never group. Their strengths are predictability in logs and files. The last two follow the locale, which is what you want on screen. If your formatting also depends on rounding to significant digits, the other formatter in this family is covered in [the post on toPrecision()](/library/javascript-number-prototype-toprecision).

Choosing locales and options

Pick the locale from the user, not from the machine. For a web application that means the language preference sent with the request, or an explicit setting in the user's profile, and it means passing the same value everywhere a number is shown. A page that mixes en-US in one table and the browser default in another is harder to read than one that is consistently wrong.

Pick the options from the meaning of the number. A count is a plain decimal with zero fraction digits. An amount of money is a currency with the code that matches the account, not the visitor. A ratio is a percent. A duration or a distance is a unit. Writing the intent into the options makes the output correct in every locale, because the locale supplies the symbols, the position of the sign and the separators.

For tests, pass the locale explicitly and compare structure where you can. Differences in the whitespace characters used by some locales, and in rarely used data between ICU versions, can break an exact-string assertion after a runtime upgrade, so use the same Node.js version in tests and in production, and keep the number of exact-string assertions small.

Where toLocaleString() stops being the right tool

Do not use it for anything a program reads back. Logs, CSV files, JSON, URLs and database values need a stable format, so use String(), toFixed() or the raw number, and format for people only at the last step. Do not leave the locale implicit in code that must behave the same everywhere, which includes tests and server rendering. Do not expect it to parse: there is no inverse method, and turning "1.234,5" back into a number requires you to know the locale and strip separators yourself.

Be careful with exact money as well. Formatting does not fix a floating-point sum, so keep amounts in integer minor units and divide only when displaying. Finally, remember that output differs between engines and ICU versions in details such as the space characters used by some locales, so a snapshot test that pins the exact string will break on upgrades. Test for the structure you care about, or pin the version.

The method is specified at Number.prototype.toLocaleString in ECMAScript and documented on the MDN toLocaleString page.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.