Sometimes, we want to round numbers with JavaScript parseFloat.
In this article, we’ll look at how to round numbers with JavaScript parseFloat.
How to round numbers with JavaScript parseFloat?
To round numbers with JavaScript parseFloat, we use the toFixed
method.
For instance, we write
const rounded = parseFloat(num.toFixed(2));
to call toFixed
on the num
number with 2 to return a number string rounded to 2 decimal places.
Then we call parseFloat
to convert the string to a number rounded to 2 decimal places.
Conclusion
To round numbers with JavaScript parseFloat, we use the toFixed
method.