More JavaScript answers

by Danny
Published 2 min read

Format prices in JavaScript with Intl.NumberFormat and style currency. Covers locales, currency codes, decimal places, compact amounts and why you should avoid toFixed.

The short answer

#

Use Intl.NumberFormat with style: 'currency' and an ISO 4217 currency code. It adds the right symbol, thousands separators and decimal places for the locale you give it.


_10
const gbp = new Intl.NumberFormat('en-GB', {
_10
style: 'currency',
_10
currency: 'GBP',
_10
});
_10
_10
gbp.format(1234.5); // "£1,234.50"
_10
_10
(1234.5).toLocaleString('de-DE', { style: 'currency', currency: 'EUR' });
_10
// "1.234,50 €"

Why it happens

#

A price looks simple, but the rules change with the reader. The symbol can go before or after the number, the decimal separator can be a point or a comma, thousands can be grouped with commas, points or spaces, and some currencies, such as the Japanese yen, have no minor unit at all. Writing '£' + price.toFixed(2) gets all of this right for one country and wrong for the rest.

Intl.NumberFormat is built into every modern browser and Node. It knows these rules for each locale and currency, so you only choose the locale (how it looks) and the currency (what the money is).

How to fix it

#

Create a formatter and reuse it

The locale controls the format and the currency code controls the symbol and decimals. They are separate on purpose: a British reader looking at a price in US dollars should see US$ in a British layout.


_10
const formatPrice = new Intl.NumberFormat('en-GB', {
_10
style: 'currency',
_10
currency: 'USD',
_10
}).format;
_10
_10
formatPrice(49.99); // "US$49.99"
_10
_10
new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' })
_10
.format(1500); // "¥1,500"

Creating a formatter has a cost, so make one per locale and currency and reuse it, for example at module level.

Control decimal places

Currencies have a default number of decimals. You can change it, for example to show whole pounds on a pricing page.


_10
const wholePounds = new Intl.NumberFormat('en-GB', {
_10
style: 'currency',
_10
currency: 'GBP',
_10
maximumFractionDigits: 0,
_10
});
_10
_10
wholePounds.format(1299.99); // "£1,300"

When you lower maximumFractionDigits below the currency default, also set minimumFractionDigits to the same or lower value if you hit a range error in older engines.

Change how the currency is shown

currencyDisplay switches between the symbol, a narrow symbol, the ISO code and the full name.


_10
const amount = 20;
_10
_10
new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'USD', currencyDisplay: 'narrowSymbol' }).format(amount);
_10
// "$20.00"
_10
_10
new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'USD', currencyDisplay: 'code' }).format(amount);
_10
// "USD 20.00"
_10
_10
new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'USD', currencyDisplay: 'name' }).format(amount);
_10
// "20.00 US dollars"

Show negative amounts as accounts do

Finance screens often show losses in brackets. Set currencySign to "accounting".


_10
new Intl.NumberFormat('en-US', {
_10
style: 'currency',
_10
currency: 'USD',
_10
currencySign: 'accounting',
_10
}).format(-42); // "($42.00)"

Use compact notation for large figures

For dashboards, notation: 'compact' shortens big numbers.


_10
new Intl.NumberFormat('en-GB', {
_10
style: 'currency',
_10
currency: 'GBP',
_10
notation: 'compact',
_10
}).format(2_500_000); // "£2.5M"

Common pitfalls

#
  • Storing money as floating-point pounds. 0.1 + 0.2 is not exactly 0.3. Store amounts as integers in the minor unit (pence or cents) and divide only when formatting.
  • Using toFixed(2) for display. It rounds, but it adds no symbol or separators and ignores locale.
  • Hard-coding two decimals. Some currencies use zero or three, and Intl already knows which.
  • Assuming the output uses ordinary spaces. Some locales insert a non-breaking or narrow non-breaking space, so string comparisons in tests can fail. Compare with formatToParts or normalise the spaces first.
  • Formatting on the server and the client with different locales or ICU data, which can cause hydration mismatches. Pass the locale explicitly.
#

Was this resource helpful?