Sometimes, we want to format a rounded number to N decimals with JavaScript.
In this article, we’ll look at how to format a rounded number to N decimals with JavaScript.
How to format a rounded number to N decimals with JavaScript?
To format a rounded number to N decimals with JavaScript, we can use the number toFixed
method.
For instance, we write
const myNumber = 2;
const numStr = myNumber.toFixed(2);
to call myNumber.toFixed
with 2 to return myNumber
formatted to 2 decimal places as a string.
Conclusion
To format a rounded number to N decimals with JavaScript, we can use the number toFixed
method.