Sometimes, we want to round up to the next multiple of by with JavaScript.
In this article, we’ll look at how to round up to the next multiple of by with JavaScript.
Use the Math.ceil Method
We can use the Math.ceil
method to round up to the next multiple of 5 with JavaScript.
For instance, we can write:
const round5 = (x) => {
return Math.ceil(x / 5) * 5;
}
console.log(round5(18))
We create the round5
function to return the number x
divided by 5.
Then we round that number up to the nearest integer with Math.ceil
.
Next, we multiply that by 5 to get x
rounded up to the nearest integer.
Therefore, the console log logs 20.
Conclusion
We can use the Math.ceil
method to round up to the next multiple of 5 with JavaScript.