The toFixed()
method rounds the given number to a specified number of decimals. It will add zeroes if the given number of decimals is higher than the number.
We will use the following syntax to call this method.
number.toFixed(x)
It will take the number of decimals as a parameter, which is optional. The default is 0
.
It returns a string with or without decimals according to the given parameter. Let's take a look at an example of this.
//given numberlet num = 5.56789;//use of toFixed method without parameterlet n = num.toFixed();console.log(n)//use of toFixed method with parametern = num.toFixed(10);console.log(n)
num
.0
decimals using the toFixed()
method without passing any parameter.10
decimals using the toFixed()
method by passing 10
as a parameter. It will add zeroes since we pass the decimals higher than the number.