How to round up to the nearest 0.05 in JavaScript?

Sometimes, we want to round up to the nearest 0.05 in JavaScript.

In this article, we’ll look at how to round up to the nearest 0.05 in JavaScript.

How to round up to the nearest 0.05 in JavaScript?

To round up to the nearest 0.05 in JavaScript, we can use the Math.ceil and toFixed methods.

For instance, we write:

const number = 20.22
const rounded = (Math.ceil(number * 20) / 20).toFixed(2)
console.log(rounded)

to multiply number by 20.

Then we round that up with Math.ceil.

Next, we divide the rounded number by 20.

And then we call toFixed with 2 to return the number as a string rounded to 2 decimal places.

Therefore, rounded is "20.25".

Conclusion

To round up to the nearest 0.05 in JavaScript, we can use the Math.ceil and toFixed methods.