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.
_10const gbp = new Intl.NumberFormat('en-GB', {_10 style: 'currency',_10 currency: 'GBP',_10});_10_10gbp.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.
_10const formatPrice = new Intl.NumberFormat('en-GB', {_10 style: 'currency',_10 currency: 'USD',_10}).format;_10_10formatPrice(49.99); // "US$49.99"_10_10new 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.
_10const wholePounds = new Intl.NumberFormat('en-GB', {_10 style: 'currency',_10 currency: 'GBP',_10 maximumFractionDigits: 0,_10});_10_10wholePounds.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.
_10const amount = 20;_10_10new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'USD', currencyDisplay: 'narrowSymbol' }).format(amount);_10// "$20.00"_10_10new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'USD', currencyDisplay: 'code' }).format(amount);_10// "USD 20.00"_10_10new 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".
_10new 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.
_10new 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.2is not exactly0.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
Intlalready 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
formatToPartsor 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.
Related reading
#Was this resource helpful?