Currency format

2 snippets in JavaScript

Also written as format currency

JSJavaScript

Formatting Decimals

JS · Numbers & Math
Syntax
num.toFixed(digits)
num.toPrecision(precision)
Example
const total = 29.5;
console.log(total.toFixed(2));       // "29.50"
console.log((0.1 + 0.2).toFixed(2)); // "0.30"

const big = 123456.789;
console.log(big.toPrecision(6));     // "123457"

Note toFixed() returns a STRING, not a number. Wrap in Number() or use + to convert back: +total.toFixed(2)

Locale Number Formatting

JS · Numbers & Math
Syntax
new Intl.NumberFormat(locale, options).format(n)
Example
const price = 1234567.89;
console.log(new Intl.NumberFormat("en-US", {
  style: "currency", currency: "USD"
}).format(price));
// "$1,234,567.89"

console.log(new Intl.NumberFormat("de-DE").format(price));
// "1.234.567,89"

Note Intl.NumberFormat handles thousands separators, currency symbols, percentages, and units correctly for any locale.

Frequently asked questions

How does JavaScript handle currency format?
JavaScript covers this with 2 copy-ready snippets on this page. The "Formatting Decimals" snippet in JavaScript uses `num.toFixed(digits)`.
Which code does the JavaScript example use?
The "Formatting Decimals" snippet uses `num.toFixed(digits)`, from the Numbers & Math section of the JavaScript cheat sheet.
What other JavaScript snippets are shown for "currency format"?
Besides "Formatting Decimals", this page also shows "Locale Number Formatting".
Is there anything to watch out for?
Yes. For "Formatting Decimals": toFixed() returns a STRING, not a number. Wrap in Number() or use + to convert back: +total.toFixed(2)