The toFixed() method is used to format a number using fixed-point notation. It can be used to format a number with a specific number of digits to the right of the decimal.
const number = 28.319921 const rounded_number = number.toFixed(2) // Output: 28.32
.toFixed(2) method convert the 28.319921 number into number with only 2 decimal points.
const number = 28 const rounded_number = number.toFixed(3) // Output: 28.000
.toFixed(3) method keeping three decimals even though they’re zero.
Now, we will convert the number with decimal points to a rounded whole number. If we give a value of
0 to the
.toFixed() method, it will round the whole number. Also If there is no parameter passed in the toFixed() method then it doesn’t display any digits after the decimal place.
const number = 28.319921 const rounded_number = number.toFixed(0) // Output: 28
That’s it for today.
Thank you for reading. Happy Coding..!!