Javascript: Round up to the next multiple of 5

104,323

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

Share:
104,323

Related videos on Youtube

Amit Erandole
Author by

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, 2022

Comments

  • Amit Erandole
    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 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?

    • user2357112
      user2357112 over 10 years
      Should round5(5) give 5, or 10?
    • Martin Wilson
      Martin Wilson over 10 years
      How about: divide x by 5, round up to the nearest integer (using the Math.ceil function) and then multiply by 5?
    • Amit Erandole
      Amit Erandole over 10 years
      round5(5) should give 5
  • Amit Erandole
    Amit Erandole over 10 years
    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.
  • pawel
    pawel over 10 years
    ReferenceError: int is not defined. Maybe you wanted parseInt, but this wouldn't be necessary since Math.floor returns a number.
  • Michael Krelin - hacker
    Michael Krelin - hacker over 10 years
    Well, it does round up to the whole integer here, @AmitErandole ;)
  • zx81
    zx81 almost 10 years
    +1 for compact and efficient... and it will round to 10, right? :)
  • TheCuBeMan
    TheCuBeMan over 9 years
    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...
  • Nicolas
    Nicolas almost 9 years
    Watch out for input of x=0... This will cause an error.
  • pawel
    pawel almost 9 years
    @Nicolas for x=0 it returns 0 as expected.
  • Nicolas
    Nicolas almost 9 years
    Sorry, my bad - a "divide by zero" waved it's arms in front of me but it isn't the case.
  • gfullam
    gfullam about 5 years
    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)
  • Spencer Stolworthy
    Spencer Stolworthy over 4 years
    This can be accomplished more simply by using Math.round
  • AlexG
    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
    DoomGoober almost 3 years
    Thanks for your solution, my unique situation needed a round up that didn't use floating point math but could use modulo.
  • Mark Dickinson
    Mark Dickinson over 2 years
    This appears to be answering a different question.
  • ABCD.ca
    ABCD.ca about 2 years
    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