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,5892 gold badges30 silver badges48 bronze badges
asked Sep 23, 2013 at 6:57
Amit ErandoleAmit Erandole
11.5k22 gold badges64 silver badges102 bronze badges
3
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.
answered Sep 23, 2013 at 7:03
9
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
answered Sep 28, 2019 at 14:54
1
Like this?
function roundup5[x] { return [x%5]?x-x%5+5:x }
answered Sep 23, 2013 at 7:05
1
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 40
// 42 => 40
// 43 => 45
// 44 => 45
// 45 => 45
// 46 => 45
// 47 => 45
// 48 => 50
// 49 => 50
// 50 => 50
answered Sep 21, 2017 at 8:34
AymKdnAymKdn
2,98922 silver badges23 bronze badges
1
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
answered Mar 16, 2016 at 9:27
// 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;
}
answered Jul 11, 2018 at 11:43
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!!!
answered Oct 16, 2018 at 10:14
ParitParit
923 bronze badges
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
answered Feb 12 at 4:58
KevinBuiKevinBui
75311 silver badges26 bronze badges
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;
answered Jun 20 at 16:13
briankipbriankip
2,4062 gold badges22 silver badges26 bronze badges
if[ x % 5 == 0 ] {
return int[ Math.floor[ x / 5 ] ] * 5;
} else {
return [ int[ Math.floor[ x / 5 ] ] * 5 ] + 5;
}
maybe?
answered Sep 23, 2013 at 7:03
MyFantasy512MyFantasy512
6821 gold badge9 silver badges20 bronze badges
1
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
}
answered Nov 18, 2021 at 17:46
1