Sub topic 9
Displaying Numbers: toFixed, toPrecision, toString(radix), and Intl.NumberFormat
This chapter assumes you know how JavaScript stores numbers as binary floating-point values (covered earlier) and that you can run code in a browser console or Node.js. Every method here returns a string. You use these methods to show a number to a person or to write it out in another base. They do not change the number you calculate with.
Fixed Decimals with toFixed
toFixed(digits) rounds a number to a set number of digits after the decimal point. It accepts 0 to 100 digits and throws a RangeError for any value outside that range.
console.log((3.14159).toFixed(2)); // "3.14"
console.log((1234.5678).toFixed(1)); // "1234.6"
console.log((7).toFixed(2)); // "7.00"
console.log(typeof (3.14159).toFixed(2)); // "string"
// The rounding uses the stored binary value, not the decimal you typed
console.log((1.005).toFixed(2)); // "1.00"
// Convert back only if you really need a number
console.log(Number((2.345).toFixed(1)) + 1); // 3.31.005 is actually stored as a value slightly below 1.005, so it rounds down. Do not use toFixed to round money before you calculate with it. Keep the full value, or work in whole cents, and format only at the end.
Significant Digits with toPrecision
toPrecision(digits) counts significant digits instead of decimal places. It accepts 1 to 100. When the number's magnitude does not fit in that many digits, it switches to exponent notation.
console.log((0.000123).toPrecision(2)); // "0.00012"
console.log((3.14159).toPrecision(3)); // "3.14"
console.log((123456).toPrecision(2)); // "1.2e+5"Other Bases with toString(radix)
toString(radix) writes the number in any base from 2 to 36. The method call needs parentheses around an integer literal, because 42.toString() is a syntax error. The parser reads the dot as a decimal point.
console.log((255).toString(16)); // "ff"
console.log((255).toString(2)); // "11111111"
console.log((-255).toString(16)); // "-ff"
console.log((0.5).toString(2)); // "0.1"
console.log(1_000_000 === 1000000); // trueNumeric separators like 1_000_000 only make source code easier to read. They never appear in output.
Locale-Aware Output with Intl.NumberFormat
Intl.NumberFormat handles digit grouping, decimal marks, currency, percentages, and compact notation according to the user's locale. If you format many values, create the formatter once and reuse it.
const usd = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
console.log(usd.format(1234.5)); // "$1,234.50"
console.log(new Intl.NumberFormat("de-DE").format(1234567.891)); // "1.234.567,891"
console.log(new Intl.NumberFormat("en-IN").format(1234567)); // "12,34,567"
const pct = new Intl.NumberFormat("en-US", { style: "percent", maximumFractionDigits: 1 });
console.log(pct.format(0.256)); // "25.6%"
const compact = new Intl.NumberFormat("en-US", { notation: "compact" });
console.log(compact.format(1234567)); // "1.2M"Some locales put non-breaking spaces in their output, for example between the amount and the € sign. Tests that compare against a string typed with an ordinary space will fail.
Common Errors
| Mistake | Result | Fix |
|---|---|---|
| Adding to a `toFixed` result | String concatenation | Wrap it in `Number()` |
| Rounding money with `toFixed` before calculating | Errors pile up | Format only for display |
| `42.toString(2)` | SyntaxError | `(42).toString(2)` |
| Hand-built thousands separators | Wrong in other locales | `Intl.NumberFormat` |
Exercise
What does (0.5).toFixed(0) + (255).toString(16) print, and why?
Show answer
It prints "1ff" in Node.js and modern browsers. 0.5 is exactly representable, so toFixed(0) rounds it away from zero to "1". (255).toString(16) is "ff". Both values are strings, so + joins them instead of adding.
Exercise
Write a formatter that displays 0.0731 as "7.3%" in US English.
Show answer
new Intl.NumberFormat("en-US", { style: "percent", maximumFractionDigits: 1 }).format(0.0731) returns "7.3%".