Number.prototype.toFixed()
Formats a number using fixed-point notation with a set number of decimals.
Syntax
num.toFixed(digits) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
digits | number | No | Number of digits after the decimal point (0 to 100). Defaults to 0. |
Returns
string — A string representation of the number with the specified decimals.
Throws
RangeError— digits is outside 0 to 100.
Examples
console.log((3.14159).toFixed(2));
Output
3.14
console.log((1.005).toFixed(2));
Output
1.00
Notes
Returns a string, not a number. Floating-point representation can cause
surprising rounding (e.g. 1.005 -> "1.00"). For currency, consider
`Intl.NumberFormat`.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |