186

I need a utility function that takes in an integer value (ranging from 2 to 5 digits in length) that rounds up to the next multiple of 5 instead of the nearest multiple of 5. Here is what I got:

function round5(x)
{
    return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
}

When I run round5(32), it gives me 30, where I want 35.
When I run round5(37), it gives me 35, where I want 40.

When I run round5(132), it gives me 130, where I want 135.
When I run round5(137), it gives me 135, where I want 140.

etc...

How do I do this?

Passerby
  • 9,715
  • 2
  • 33
  • 50
Amit Erandole
  • 11,995
  • 23
  • 65
  • 103

10 Answers10

417

This will do the work:

function round5(x)
{
    return Math.ceil(x / 5) * 5;
}

It's just a variation of the common rounding number to nearest multiple of x function Math.round(number/x)*x, but using .ceil instead of .round makes it always round up instead of down/up according to mathematical rules.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
pawel
  • 35,827
  • 7
  • 56
  • 53
  • 1
    could you explain a little about how you came to this solution so fast? I thought Math.ceil only rounds up decimals to whole integers. – Amit Erandole Sep 23 '13 at 07:07
  • 2
    Well, it does round up to the whole integer here, @AmitErandole ;) – Michael Krelin - hacker Sep 23 '13 at 07:12
  • 1
    +1 for compact and efficient... and it will round to 10, right? :) – zx81 Jun 09 '14 at 10:19
  • I'd add another parameter to this function, indicating the "rounder", so the original number can be rounded to whatever we set in the function call and not only fixed 5... – TheCuBeMan Oct 22 '14 at 11:27
  • Watch out for input of x=0... This will cause an error. – Nicolas May 13 '15 at 07:39
  • @Nicolas for `x=0` it returns `0` as expected. – pawel May 13 '15 at 07:47
  • Sorry, my bad - a "divide by zero" waved it's arms in front of me but it isn't the case. – Nicolas May 14 '15 at 08:30
  • 4
    I love this solution! I implemented it with a closure for conveniently changing the multiple inline as needed: `const roundToNearestMultipleOf = m => n => Math.round(n/m)*m` Usage: `roundToNearestMultipleOf(5)(32)` – gfullam Feb 21 '19 at 19:25
  • Arrow function version: `const ceilToNearest = (value, increment) => Math.ceil(value / increment) * increment`. TypeScript version: const ceilToNearest = (value: number, increment: number): number => Math.ceil(value / increment) * increment – ABCD.ca Feb 11 '22 at 06:12
44
const roundToNearest5 = x => Math.round(x / 5) * 5

This will round the number to the nearest 5. To always round up to the nearest 5, use Math.ceil. Likewise, to always round down, use Math.floor instead of Math.round. You can then call this function like you would any other. For example,

roundToNearest5(21)

will return:

20
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Spencer Stolworthy
  • 1,352
  • 10
  • 17
  • +1 because I needed a way to round to the *nearest* five. (OP asked for rounding to the *next* five (upwards). Thus, the accepted answer is indeed correct, @Oliver.) – AlexG Jan 21 '21 at 12:36
  • Just curious why don't use `Math.round()` ?? – Kunal Tanwar Oct 22 '22 at 17:02
9

Like this?

function roundup5(x) { return (x%5)?x-x%5+5:x }
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • 1
    Thanks for your solution, my unique situation needed a round up that didn't use floating point math but could use modulo. – DoomGoober Jun 07 '21 at 19:39
4

I arrived here while searching for something similar. If my number is —0, —1, —2 it should floor to —0, and if it's —3, —4, —5 it should ceil to —5.

I came up with this solution:

function round(x) { return x % 5 < 3 ? (x % 5 === 0 ? x : Math.floor(x / 5) * 5) : Math.ceil(x / 5) * 5 }

And the tests:

for (var x = 40; x < 51; x++) {
  console.log(x+"=>", x % 5 < 3 ? (x % 5 === 0 ? x : Math.floor(x / 5) * 5) : Math.ceil(x / 5) * 5)
}
// 40 => 40
// 41 => 40
// 42 => 40
// 43 => 45
// 44 => 45
// 45 => 45
// 46 => 45
// 47 => 45
// 48 => 50
// 49 => 50
// 50 => 50
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
AymKdn
  • 3,327
  • 23
  • 27
3
voici 2 solutions possibles :
y= (x % 10==0) ? x : x-x%5 +5; //......... 15 => 20 ; 37 => 40 ;  41 => 45 ; 20 => 20 ; 

z= (x % 5==0) ? x : x-x%5 +5;  //......... 15 => 15 ; 37 => 40 ;  41 => 45 ; 20 => 20 ;

Regards Paul

1

New answer for old question, without if nor Math

x % 5: the remainder
5 - x % 5: the amount need to add up
(5 - x % 5) % 5: make sure it less than 5
x + (5 - x % 5) % 5: the result (x or next multiple of 5)

~~((x + N - 1) / N): equivalent to Math.ceil(x / N)

function round5(x) {
 return x + (5 - x % 5) % 5;
}

function nearest_multiple_of(N, x) {
 return x + (N - x % N) % N;
}

function other_way_nearest_multiple_of(N, x) {
 return ~~((x + N - 1) / N) * N;
}


console.info(nearest_multiple_of(5,    0)); // 0
console.info(nearest_multiple_of(5, 2022)); // 2025
console.info(nearest_multiple_of(5, 2025)); // 2025

console.info(other_way_nearest_multiple_of(5, 2022)); // 2025
console.info(other_way_nearest_multiple_of(5, 2025)); // 2025
KevinBui
  • 880
  • 15
  • 27
-1

// round with precision

var round = function (value, precision) {
    return Math.round(value * Math.pow(10, precision)) / Math.pow(10, precision);
};

// round to 5 with precision

var round5 = (value, precision) => {
    return round(value * 2, precision) / 2;
}
Dennis T
  • 109
  • 4
-1
const fn = _num =>{
    return Math.round(_num)+ (5 -(Math.round(_num)%5))
}

reason for using round is that expected input can be a random number.

Thanks!!!

Parit
  • 98
  • 3
-1

I solved it using a while loop, or any other loop for that matter. What is important is to increase the number say n, until n % 5 == 0; Something like this

while(n % 5 != 0) {
    n++;
}
return n;
briankip
  • 2,502
  • 2
  • 23
  • 26
-3
if( x % 5 == 0 ) {
    return int( Math.floor( x / 5 ) ) * 5;
} else {
    return ( int( Math.floor( x / 5 ) ) * 5 ) + 5;
}

maybe?

MyFantasy512
  • 690
  • 1
  • 9
  • 20
  • 1
    ReferenceError: `int` is not defined. Maybe you wanted `parseInt`, but this wouldn't be necessary since `Math.floor` returns a number. – pawel Sep 23 '13 at 07:07