Rounding Up a Number to Nearest Multiple of 5

Rounding up a number to nearest multiple of 5

int roundUp(int n) {
return (n + 4) / 5 * 5;
}

Note - YankeeWhiskey's answer is rounding to the closest multiple, this is rounding up. Needs a modification if you need it to work for negative numbers. Note that integer division followed by integer multiplication of the same number is the way to round down.

Round Numerical Values to Nearest Multiple of 5

You can use this awk:

s='25% 80% 22% 67% 45% 30%'
awk '{for (i=1; i<=NF; i++) $i = int( ($i+2) / 5) * 5 "%"} 1' <<< "$s"

25% 80% 20% 65% 45% 30%

Rounding off a positive number to the next nearest multiple of 5

To round up the general form should be:

((n + denominator -1) / denominator )* denominator 

so in your case:

int round = ((grades[j] + 4)/5) * 5;

The reason we deduct 1 from the denominator is to handle exact multiples of the rounding value for instance:

((70 + 4) / 5) * 5

would yield 70

Rounding up to the nearest multiple of 5

If the remainder is 3 or above, you simply need to add 5 and subtract the remainder, which can be done with grade += 5 - remainder. Also note that you don't need your second else if conditional at all, as you only want to modify grade if the remainder is greater than or equal to 3. Finally, you need to make sure that your function actually returns grade with return grade.

This can be seen in the following:

function roundGrade(grade) {  const remainder = grade % 5;  if (grade < 38) {    return "fail";  } else if (remainder >= 3) {    grade += 5 - remainder;  }  return grade;}
console.log(roundGrade(71));console.log(roundGrade(74));

Java: round to nearest multiple of 5 (either up or down)

haven't tested it, but 5*(Math.round(f/5)); should work

Javascript: Round up to the next multiple of 5

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.

Round up to nearest multiple of five in PHP

This can be accomplished in a number of ways, depending on your preferred rounding convention:

1. Round to the next multiple of 5, exclude the current number

Behaviour: 50 outputs 55, 52 outputs 55

function roundUpToAny($n,$x=5) {
return round(($n+$x/2)/$x)*$x;
}

2. Round to the nearest multiple of 5, include the current number

Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 50

function roundUpToAny($n,$x=5) {
return (round($n)%$x === 0) ? round($n) : round(($n+$x/2)/$x)*$x;
}

3. Round up to an integer, then to the nearest multiple of 5

Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 55

function roundUpToAny($n,$x=5) {
return (ceil($n)%$x === 0) ? ceil($n) : round(($n+$x/2)/$x)*$x;
}

How can I round up to the nearest multiple of the specified number?

It's enough to add the missing mod45:

int upperround45(int i) {
int temp = i%45;
//For the algorithm we wish the rest to be how much more than last multiple of 45.
if (temp < 0 )
temp = 45 + temp;
if (temp == 0) {
return i;
} else {
return i + 45 - temp;
}
}

EDIT:

In general:

int upperround(int num, int base) {
int temp = num%base;
if (temp < 0 )
temp = base + temp;
if (temp == 0)
return num;
return num + base - temp;


Related Topics



Leave a reply



Submit