How to format numbers and prices in JavaScript — separators, currencies and the toFixed trap

Try this in your browser console: (1.005).toFixed(2). The answer is "1.00". Not 1.01, like you learned at school. In a shopping cart that's a cent lost; in a report it's a mismatch your accountant will find.
And a second one: format 1234.5 properly for Russian and you get 1 234,50 ₽. Compare it in a test with a string you typed by hand — and the test fails. Even though the characters look identical on screen.
Both problems are solved by one built-in tool — Intl.NumberFormat. No libraries. Let's go step by step.
Step 0: format only when displaying
The main rule, before any code. A number in your program stays a number: you calculate with it, add it up and store it in the database as a number. The pretty string with separators and a currency sign appears at the very last moment — when you show it to a person.
Format too early and you can't calculate any more: "$1,234.50" is already a string, not a number. Turning it back is a separate pain, covered in how to convert a string to a number. There, parseFloat("1 234,50") cheerfully returns 1.
Step by step
-
Thousands separators. Create a formatter with a locale and hand it a number:
const num = new Intl.NumberFormat("en-US"); num.format(1234567.891); // "1,234,567.891"Commas between thousands and a dot for decimals — automatically. With
ru-RUyou'd get1 234 567,891. -
A price in dollars. Add the currency style and a currency code:
const usd = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", }); usd.format(1234.5); // "$1,234.50" usd.format(1990); // "$1,990.00"The currency code has three letters:
USD,EUR,RUB,KZT. The engine picks the sign and where it goes. Rubles in a Russian locale come out as1 234,50 ₽; dollars in a Russian locale as1 234,50 $.Notice: locale and currency are independent settings. The locale decides how separators and decimals look; the currency decides which money it is. That can surprise you: tenge in
en-UScomes out asKZT 15,000.00— in letters — while the Kazakh localekk-KZshows the₸sign. Eyeball your pair before shipping it. -
Drop the cents if you don't need them. For prices like "$1,990", limit the decimal places:
const usdShort = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 0, }); usdShort.format(1990.6); // "$1,991"The formatter rounded by itself — and, unlike
toFixed, the way a human expects: formatting1.005with two decimals gives1.01. -
Percentages and "millions". There's a
percentstyle for a share between 0 and 1, and compact notation for big numbers:new Intl.NumberFormat("en-US", { style: "percent" }).format(0.256); // "26%" new Intl.NumberFormat("en-US", { notation: "compact" }).format(1234567); // "1.2M"Note: percent takes
0.256, not25.6. Pass 25.6 and you get "2,560%". -
Create the formatter once.
new Intl.NumberFormat(...)isn't free. If you format prices in a list of a thousand products, declare the formatter once outside the loop and call its.format()inside. -
Fix your tests. Between thousands and before
₽, the Russian locale doesn't put a regular space — it puts a non-breaking one, so the number never gets split across lines. It looks the same, but it's a different character. We checked on Node 22:"1 234,50 ₽"typed on a keyboard is not equal to the formatter's output. In tests, compare against the same formatter's output, or replace non-breaking spaces before comparing:s.replace(/ /g, " ").
Why toFixed lies
The number 1.005 isn't stored exactly in memory — it's slightly less, about 1.00499999…. That's how fractional numbers work in almost every language. toFixed honestly rounds what's in memory and gets 1.00.
Intl.NumberFormat rounds the number the way it's written, so 1.005 goes up, as a human expects. For displaying money, that's one more reason to use it instead of toFixed.
And for calculations with money, it's safer to store amounts as whole numbers of cents: 199050 instead of 1990.50. Add up cents, and divide into dollars only for display.
What you end up with
One small function for the whole project:
const usdFormatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0,
});
export function price(value) {
return usdFormatter.format(value);
}
Everywhere in the UI — price(item.cost). Prices look the same on every screen. Want a Russian version of the site? Change the locale in one place. Ask your AI agent to replace every home-made "$" + x.toFixed(2) in the project with this price() — that's one clear task.
How do I do this in Python?
The built-in f"{x:,.2f}" gives the American style 1,234,567.89. For other locales, install the babel library — it has format_currency with locale support.
Which locale should I use?
Use the user's locale: navigator.language in the browser. Then everyone sees numbers in the format they're used to — commas for some, spaces and decimal commas for others.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





