Number.prototype.toFixed()
toFixed(digits) string · Updated March 13, 2026 · Number toFixed() formats a number to a specified number of decimal places, returning the result as a string. This is essential for financial calculations, currency display, and anywhere precise decimal control matters.
Syntax
num.toFixed(digits)
Parameters:
digits— Number of decimal places (0 to 100). Values 0-20 are most widely supported.
Returns: A string representation of the number with exactly digits decimal places.
How It Works
toFixed() rounds the number to the specified precision rather than simply truncating it. The last digit is rounded according to standard rounding rules — if the next digit is 5 or greater, it rounds up.
(2.35).toFixed(1) // "2.4" — rounds up
(2.34).toFixed(1) // "2.3" — rounds down
(1.235).toFixed(2) // "1.24" — rounds up
(1.234).toFixed(2) // "1.23" — rounds down
Why It Returns a String
This is intentional. JavaScript’s number type follows IEEE 754 floating-point, which can’t precisely represent many decimal values. By returning a string, toFixed() preserves the formatted representation without introducing floating-point errors:
const price = 19.99;
const formatted = price.toFixed(2); // "19.99" (string)
// Use with currency symbols:
'$' + (9.99).toFixed(2) // "$9.99"
Common Use Cases
Currency Formatting
const amounts = [19.99, 5.5, 100, 0.1];
amounts.forEach(amount => {
console.log('$' + amount.toFixed(2));
});
// $19.99
// $5.50
// $100.00
// $0.10
Percentage Display
function formatPercent(value, total) {
const percent = (value / total) * 100;
return percent.toFixed(1) + '%';
}
formatPercent(7, 20) // "35.0%"
formatPercent(1, 3) // "33.3%"
Precision Control
const measurements = [1.23456, 9.8765, 0.001];
console.log(measurements.map(n => n.toFixed(2)));
// ["1.23", "9.88", "0.00"]
console.log((Math.PI).toFixed(4)); // "3.1416"
Limits and Compatibility
| Digits | Support | Notes |
|---|---|---|
| 0-20 | ✅ Universal | Works in all browsers and environments |
| 21-100 | ⚠️ Varies | May throw RangeError in older environments |
When digits is omitted, it defaults to 0:
(42).toFixed() // "42"
(42.7).toFixed() // "43" — rounds to whole number
Gotchas
Floating-Point Precision
Binary floating-point can’t exactly represent all decimals:
(0.3).toFixed(20) // "0.29999999999999998890"
For critical financial work, consider using integer math or libraries like decimal.js.
Rounding Edge Cases
Some values exhibit floating-point rounding quirks:
(2.55).toFixed(1) // "2.5" — should be "2.6" but precision limits prevent it
See Also
- number::toPrecision — Format to precision
- number::toString — Convert to string