Given a positive integer n and the task is to round the number to the next whole number divisible by 5. There are various methods to Round off a number to the next multiple of 5 using JavaScript.
- Using Math.ceil() method
- Using Math.floor() method
[Approach 1]: The approach involves scaling the number down, rounding it up, and then scaling it back to the nearest multiple of 5.
- Take the number in a variable.
- Divide it by 5 and get the decimal value.
- Take the ceil value of the decimal value by using math.ceil().
- Multiply it by 5 to get the result.
function r1(x) {
return Math.ceil(x / 5) * 5;
}
var n = 34;
console.log(r1(n));
Output
35
[Approach 2]: This approach checks divisibility by 5 and adjusts the number only when needed to reach the next multiple.
- Take the number in a variable.
- If it is divisible by 5, return the same number.
- Else divide it by 5, take the floor value and again multiply it by 5 and add 5 as well.
function r2(x) {
if (x % 5 === 0) {
return Math.floor(x / 5) * 5;
} else {
return (Math.floor(x / 5) * 5) + 5;
}
}
var n = 34;
console.log(r2(n));
Output
35