toFixed()
is a Number
method that is used to format a number using fixed-point notation. toFixed()
formats a number to a given length after the decimal point by rounding off to reduce the length or by adding zeros to increase the length.
num.toFixed(len)
num
: The number to be formatted using fixed-point notation.len
: The number of digits after the decimal point in which num
will be formatted. len
is an optional parameter.Note:
- If
len
is not passed as a parameter, the defaultlen
islen = 0
. The number is rounded to a whole number.len
must be in the range 1 to 100. Otherwise, RangeError is thrown.
The toFixed()
method is supported by the following browsers:
var num = 15.199console.log("15.199 to precision of no length is: ",num.toFixed());console.log("15.199 to precision of length 3 is: ",num.toFixed(1));console.log("15.199 to precision of length 7 is: ",num.toFixed(7));
Free Resources