Javascript: Round up to the next multiple of 5

JavascriptMathRounding

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

Like this?

function roundup5(x) { return (x%5)?x-x%5+5:x }

Solution 4 - Javascript

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 - Javascript

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

Solution 6 - Javascript

// 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;
}

Solution 7 - Javascript

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!!!

Solution 8 - Javascript

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

Solution 9 - Javascript

function gradingStudents(grades) {
    // Write your code here
    const roundedGrades = grades.map(grade => {
       if(grade >= 38 && grade %5 >=3){
            while(grade % 5 != 0){
                grade++
            }
        }
        return grade
    }) 
    return roundedGrades
}

Solution 10 - Javascript

if( x % 5 == 0 ) {
    return int( Math.floor( x / 5 ) ) * 5;
} else {
    return ( int( Math.floor( x / 5 ) ) * 5 ) + 5;
}

maybe?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmit ErandoleView Question on Stackoverflow
Solution 1 - JavascriptpawelView Answer on Stackoverflow
Solution 2 - JavascriptSpencer StolworthyView Answer on Stackoverflow
Solution 3 - JavascriptMichael Krelin - hackerView Answer on Stackoverflow
Solution 4 - JavascriptAymKdnView Answer on Stackoverflow
Solution 5 - JavascriptPaul NeulatView Answer on Stackoverflow
Solution 6 - JavascriptDennis TView Answer on Stackoverflow
Solution 7 - JavascriptParitView Answer on Stackoverflow
Solution 8 - JavascriptKevinBuiView Answer on Stackoverflow
Solution 9 - JavascriptSiddiqui AffanView Answer on Stackoverflow
Solution 10 - JavascriptMyFantasy512View Answer on Stackoverflow