All stacks / Intents / Currency format Also written as format currency
Formatting Decimals JS · Numbers & Math Syntax ⧉ Copy num. toFixed ( digits)
num. toPrecision ( precision) Example ⧉ Copy const total = 29.5 ;
console . log ( total. toFixed ( 2 ));
console . log (( 0.1 + 0.2 ). toFixed ( 2 ));
const big = 123456.789 ;
console . log ( big. toPrecision ( 6 )); 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 ⧉ Copy new Intl . NumberFormat ( locale, options). format ( n) Example ⧉ Copy const price = 1234567.89 ;
console . log ( new Intl . NumberFormat ( "en-US" , {
style: "currency" , currency: "USD"
}). format ( price));
console . log ( new Intl . NumberFormat ( "de-DE" ). format ( price));
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) Related tasks