Categories
JavaScript Answers

How to round numbers with JavaScript parseFloat?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *