Round a Number Up to the Next Multiple of 5 in JavaScript

Last Updated : 22 Jan, 2026

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.

[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.
javascript
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.
javascript
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
Comment