Convert a number into a string, keeping only two decimals:
The result of n will be:
More "Try it Yourself" examples below.
The toFixed() method converts a number into a string, keeping a specified number of decimals.
Note: if the desired number of decimals are higher than the actual number, nulls are added to create the desired decimal length.
Method | |||||
---|---|---|---|---|---|
toFixed() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
x | Optional. The number of digits after the decimal point. Default is 0 (no digits after the decimal point) |
Return Value: | A String, representing a number, with the exact number of decimals |
---|---|
JavaScript Version: | 1.5 |
Convert a number, without keeping any decimals:
The result of n will be:
Convert a number which has fewer decimal places than requested:
The result of n will be: