Number.prototype.toFixed()
Formats a number with a fixed number of decimal places and returns it as a string.
toFixed()
toFixed(digits)- Added in JavaScript ES3
Parameters
| Parameter | Type | Description |
|---|---|---|
$digitsoptional | number= 0 | How many digits to show after the decimal point. Must be between 0 and 100 inclusive. |
Return value
stringA string representing the number in fixed-point notation with `digits` digits after the decimal point. Scientific notation is used instead if the number's magnitude is 10^21 or greater.
Errors
Throws a RangeError if digits is outside 0–100, and a TypeError if called on a value that is not a number.
toFixed() is the quickest way to turn 19.5 into "19.50". It pads with trailing zeros when the number has fewer decimals than you asked for, and rounds when it has more. The result is always a string, so (1.5).toFixed(2) + 1 gives "1.501", not 2.5.
The rounding is not what you were taught
Rounding happens against the binary double that is actually stored, not the decimal digits you typed. 1.005 is stored as slightly less than 1.005, so (1.005).toFixed(2) is "1.00", while (1.45).toFixed(1) is "1.4" and (2.35).toFixed(1) is "2.4". There is no way to predict these from the literal alone.
When to reach for it
Use it for display only, at the last moment before the value reaches a screen or a log line. Keep the full-precision number in your data. For anything involving currency across locales, prefer toLocaleString(), which adds grouping separators and the right decimal mark. For money that must round correctly to the cent, do arithmetic in integer minor units (cents) and only format at the end.
Very large numbers
Above 1021 the fixed-point form is abandoned and you get exponential output — (1e21).toFixed(2) is "1e+21".
Example
When to use it
- Render a cart total as "12.00" instead of "12"
- Trim a computed average to two decimals in a report row
- Fix the width of numeric output in a console table
Frequently asked questions
Why does toFixed(2) round 1.005 down to 1.00?
Because 1.005 cannot be stored exactly in binary floating point; the nearest double is slightly below 1.005, so rounding to two places gives 1.00. Round in integer minor units, or add a tiny correction based on the magnitude, if you need decimal rounding rules.
How do I get a number back instead of a string from toFixed()?
Wrap the call: Number(x.toFixed(2)) or +x.toFixed(2). toFixed() always returns a string, so unwrapped results concatenate rather than add.
Does toFixed() truncate or round?
It rounds to the nearest representable value at that precision, it does not truncate. To truncate toward zero use Math.trunc(x * 100) / 100 instead.
More examples
Reading the number back out
The string result is convenient for display but silently concatenates in arithmetic. Wrap it in Number() the moment you need to compute with it again.
const raw = 0.1 + 0.2;
console.log(raw); // 0.30000000000000004
const shown = raw.toFixed(2);
console.log(shown, typeof shown); // "0.30" string
// Concatenation, not addition, if you forget to convert back
console.log(shown + 1); // "0.301"
console.log(Number(shown) + 1); // 1.3Rounding money safely in integer cents
Summing in integer minor units keeps every intermediate value exact, so toFixed() only formats at the end instead of hiding an accumulated error.
const cents = [1999, 550, 1];
const total = cents.reduce((a, b) => a + b, 0);
console.log(total); // 2550
console.log((total / 100).toFixed(2)); // "25.50"
// Compare with accumulating floats directly
const floats = [19.99, 5.5, 0.01];
console.log(floats.reduce((a, b) => a + b, 0)); // 25.499999999999996Guarding the digits argument
Anything outside 0 through 100 is a RangeError, not a clamped value, so user-supplied precision needs validating before it reaches toFixed().
function money(n, digits) {
try {
return n.toFixed(digits);
} catch (err) {
return `${err.name}: out of range`;
}
}
console.log(money(3.14159, 3)); // "3.142"
console.log(money(3.14159, 0)); // "3"
console.log(money(3.14159, 101)); // "RangeError: out of range"
console.log(money(3.14159, -1)); // "RangeError: out of range"
Discussion