Elective course
- Previous Page toExponential()
- Next Page toLocaleString()
- Go to the Previous Level JavaScript Number Reference Manual
Course recommendation:
Definition and usage
JavaScript Number toFixed() Method
Definition and usage
Method converts a number to a string.
toFixed()This method rounds the string to the specified number of decimal places.
Note:
If the number of decimal places is higher than the number, add zeros.
let num = 5.56789; Instance
Example 1
let num = 5.56789; Example 2
Example 3
Rounded to 10 decimal places:
let num = 5.56789; let n = num.toFixed(10);
Syntax
number.toFixed(digits)
Parameter
Parameter | Description |
---|---|
digits |
Optional. Number of decimal places. Values between 0 and 20, including 0 and 20. Default is 0 (no decimals). |
Returns value
Type | Description |
---|---|
String | Representation of a number with (or without) decimals. |
Technical details
Returns value
Returns number string representation, not in exponential notation, with a fixed number of decimal places after the decimal point. digits digit places.
If necessary, the number will be rounded and can also be padded with zeros to reach the specified length.
If digits If the number is greater than le+21, this method will only call number.toString() returns a string represented in exponential notation.
thrown
Exception | Description |
---|---|
RangeError |
When digits An exception is thrown when it is too small or too large. Values between 0 and 20 will not trigger this exception. Some implementations may support a wider or narrower range of values. |
TypeError | An exception is thrown if the object called this method is not a Number. |
Browser support
Number.constructor
It is a feature of ECMAScript3 (ES3).
All browsers fully support ES3 (JavaScript 1999):
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
- Previous Page toExponential()
- Next Page toLocaleString()
- Go to the Previous Level JavaScript Number Reference Manual