Number methods format a number as a string; none of them changes the value. To call one on a literal, write (5).toString() or 5..toString(), because 5.toString() reads the dot as a decimal point.
| Method | Returns | (123.456) example |
|---|---|---|
| toFixed(digits) | fixed decimals (0-100) | toFixed(2) → "123.46" |
| toPrecision(n) | n significant digits | toPrecision(4) → "123.5" |
| toExponential(d) | scientific notation | toExponential(2) → "1.23e+2" |
| toString(radix) | digits in base 2-36 | toString(16) → "7b.74bc6a7ef9dc" |
| toLocaleString(loc, opts) | locale-aware text | "123.456" (en-US) |
console.log((1.005).toFixed(2), (1.005).toPrecision(17), 5..toString(2));
console.log((1234567.891).toLocaleString("de-DE"),
(2500).toLocaleString("en-GB", { style: "currency", currency: "GBP" }));1.00 1.0049999999999999 101 1.234.567,891 £2,500.00
(1.005).toFixed(2) gives "1.00", not "1.01", because the double nearest to 1.005 lies just below it. For money, calculate in integer cents (or BigInt) and format with Intl.NumberFormat (NumberFormat). parseInt(str, radix) is the inverse of toString(radix).