Categories
JavaScript Answers

How to round down number 2 decimal places with JavaScript?

Spread the love

Sometimes, we want to round down number 2 decimal places with JavaScript.

In this article, we’ll look at how to round down number 2 decimal places with JavaScript.

How to round down number 2 decimal places with JavaScript?

To round down number 2 decimal places with JavaScript, we can multiply the number we want to round by 100, take the floor of the product with Math.floor, and then divide the floor with 100.

For instance, we write:

const n = Math.floor(1.5675675 * 100) / 100
console.log(n)

We want to round 1.5675675 down to 2 decimal places.

To do this, we multiply 1.5675675 by 100.

Then we call Math.floor on the product to take the floor.

And then we divided that by 100.

Therefore, n is 1.56.

Conclusion

To round down number 2 decimal places with JavaScript, we can multiply the number we want to round by 100, take the floor of the product with Math.floor, and then divide the floor with 100.

By John Au-Yeung

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

One reply on “How to round down number 2 decimal places with JavaScript?”

Math.floor(2.03 * 100) / 100 will return 2.02

Correct solution

const strip = (number) => parseFloat(number.toPrecision(12));
Math.floor(strip(2.03 * 100)) / 100

Leave a Reply

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