Javascript: Round up to the next multiple of 5
Solution 1
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.
Solution 2
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
Solution 3
Like this?
function roundup5(x) { return (x%5)?x-x%5+5:x }
Solution 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
Solution 5
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
Related videos on Youtube
Amit Erandole
I am the content head of my very own web marketing agency, based out of Mumbai. I am a writer by profession but the dark side of my nature seems to have turned towards programming. I will have my vengeance!! I love learning javascript and ruby and amazed by the power of meta programming and shit I don't know but am very curious about.
Updated on July 25, 2022Comments
-
Amit Erandole almost 2 years
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 me30
, where I want 35.
When I runround5(37)
, it gives me35
, where I want 40.When I run
round5(132)
, it gives me130
, where I want 135.
When I runround5(137)
, it gives me135
, where I want 140.etc...
How do I do this?
-
user2357112 over 10 yearsShould
round5(5)
give 5, or 10? -
Martin Wilson over 10 yearsHow about: divide x by 5, round up to the nearest integer (using the Math.ceil function) and then multiply by 5?
-
Amit Erandole over 10 yearsround5(5) should give 5
-
-
Amit Erandole over 10 yearscould you explain a little about how you came to this solution so fast? I thought Math.ceil only rounds up decimals to whole integers.
-
pawel over 10 yearsReferenceError:
int
is not defined. Maybe you wantedparseInt
, but this wouldn't be necessary sinceMath.floor
returns a number. -
Michael Krelin - hacker over 10 yearsWell, it does round up to the whole integer here, @AmitErandole ;)
-
zx81 almost 10 years+1 for compact and efficient... and it will round to 10, right? :)
-
TheCuBeMan over 9 yearsI'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...
-
Nicolas almost 9 yearsWatch out for input of x=0... This will cause an error.
-
pawel almost 9 years@Nicolas for
x=0
it returns0
as expected. -
Nicolas almost 9 yearsSorry, my bad - a "divide by zero" waved it's arms in front of me but it isn't the case.
-
gfullam about 5 yearsI 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)
-
Spencer Stolworthy over 4 yearsThis can be accomplished more simply by using
Math.round
-
AlexG over 3 years+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.)
-
DoomGoober almost 3 yearsThanks for your solution, my unique situation needed a round up that didn't use floating point math but could use modulo.
-
Mark Dickinson over 2 yearsThis appears to be answering a different question.
-
ABCD.ca about 2 yearsArrow 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