Sometimes,. we want to round to a number of decimal places, but strip extra zeros with JavaScript.
In this article, we’ll look at how to round to a number of decimal places, but strip extra zeros with JavaScript.
How to round to a number of decimal places, but strip extra zeros with JavaScript?
To round to a number of decimal places, but strip extra zeros with JavaScript, we use the number toFixed
method and the parseFloat
function.
For instance, we write
const n = parseFloat((1.005).toFixed(15));
to call (1.005).toFixed(15)
to round 1.005 to 15 decimal places.
Then we pass the returned number string into parseFloat
to return the rounded number without the extra trailing zeroes.
Conclusion
To round to a number of decimal places, but strip extra zeros with JavaScript, we use the number toFixed
method and the parseFloat
function.