The toFixed() method converts a number into a string keeping a specified number of decimals.
The toFixed() method converts a number into a string keeping a specified number of decimals.
number.toFixed(x)
Parameter | Require | Description |
---|---|---|
x | Optional. | The number of digits after the decimal point. Default is 0, means no digits after the decimal point |
A String, representing a number, with the exact number of decimals
//Convert a number into a string, keeping only two decimals: var num = 5.56789; var n = num.toFixed(2); console.log(n);/*from w w w .j av a 2 s. co m*/ //Convert a number, without keeping any decimals: var num = 5.56789; var n = num.toFixed(); console.log(n); //Convert a number which has fewer decimal places than requested: var num = 5.56789; var n = num.toFixed(10); console.log(n);