Sometimes, we want to round up a number in JavaScript.
In this article, we’ll look at how to round up a number in JavaScript.
How to round up a number in JavaScript?
To round up a number in JavaScript, we can use the Math.round
method with the toFixed
method.
For instance, we write
const n = (Math.round(price * 10) / 10).toFixed(2);
to multiply price
by 10.
Then we call Math.round
to round the number.
And then we divide the rounded by 10 return the number rounded up to the nearest 10.
And then we call toFixed
with 2 to return a string with the numbered rounded to 2 decimal places.
Conclusion
To round up a number in JavaScript, we can use the Math.round
method with the toFixed
method.