Categories
JavaScript Answers

How to round money amount to the nearest 10 dollars in JavaScript?

Spread the love

Sometimes, we want to round money amount to the nearest 10 dollars in JavaScript.

In this article, we’ll look at how to round money amount to the nearest 10 dollars in JavaScript.

How to round money amount to the nearest 10 dollars in JavaScript?

To round money amount to the nearest 10 dollars in JavaScript, we can use the Math.round method.

For instance, we write:

const val = 123.45
const rounded = Math.round(val / 10) * 10;
console.log(rounded)

to round val to the nearest 10 dollars.

To do this, we first divide val by 10.

Then we round the quotient to the nearest integer with Math.round.

Finally, we divide the rounded number by 10 to return val rounded to the nearest 10 dollars.

Therefore, val is 120.

Conclusion

To round money amount to the nearest 10 dollars in JavaScript, we can use the Math.round 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 *