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.