JavaScript
Format decimal numbers for display
Use toFixed when you need a fixed number of decimal places in display text. It returns a string and should not be mistaken for a change to the precision of the stored number.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const value = 12.3;
const label = value.toFixed(2);
console.log(label);
console.log(typeof label);
console.log(value);How it works
The display label contains two digits after the decimal point, including the trailing zero. The original Number value remains 12.3. Keeping formatting at the display boundary avoids mixing human-readable text into calculations.
Limits and mistakes
Binary floating-point representation can produce rounding results that surprise people for decimal fractions. ToFixed is not an exact decimal accounting system and does not apply locale-specific separators. Use Intl.NumberFormat for localized display.
Check the result
Expect 12.30, string, and 12.3. Test values near your rounding boundaries and large values before treating the formatted result as an application-wide numerical rule.
Sources
Reference documentation for the behavior used in this guide: