Sometimes, we want to round to two decimal places with JavaScript.
In this article, we’ll look at how to round to two decimal places with JavaScript.
How to round to two decimal places with JavaScript?
To round to two decimal places with JavaScript, we can use toFixed
and the +
operator.
For instance, we write
const r = +discount.toFixed(2)
to round the discount
number to 2 decimal places by first calling toFixed
with 2 to convert it to a string that has discount
rounded to 2 decimal places.
And then we add +
in front of it to convert the rounded number string back to a number.
Conclusion
To round to two decimal places with JavaScript, we can use toFixed
and the +
operator.