Rounding Up to the Nearest Multiple of a Number

Rounding up to the nearest multiple of a number

This works for positive numbers, not sure about negative. It only uses integer math.

int roundUp(int numToRound, int multiple)
{
if (multiple == 0)
return numToRound;

int remainder = numToRound % multiple;
if (remainder == 0)
return numToRound;

return numToRound + multiple - remainder;
}

Edit: Here's a version that works with negative numbers, if by "up" you mean a result that's always >= the input.

int roundUp(int numToRound, int multiple)
{
if (multiple == 0)
return numToRound;

int remainder = abs(numToRound) % multiple;
if (remainder == 0)
return numToRound;

if (numToRound < 0)
return -(abs(numToRound) - remainder);
else
return numToRound + multiple - remainder;
}

Round to nearest multiple of a number

Add half of the multiple, then round down.

result = ((number + multiple/2) / multiple) * multiple;

or

result = number + multiple/2;
result -= result % multiple;

This rounds up if the number is exactly in the middle. You might need to tweak the calculation if you want different behaviour in that case. Also, beware overflow if number might be near the top of the type's range.

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;

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 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 number up to the nearest multiple of 3

    if(n > 0)
return Math.ceil(n/3.0) * 3;
else if( n < 0)
return Math.floor(n/3.0) * 3;
else
return 3;

Round Up to the nearest multiple of 12

Use Math.ceil()

var n = 13;
var next = Math.ceil(n/12) * 12;

How do I round an integer down to the nearest multiple of a number?

I'm trying to round an integer to the nearest multiple of a number.
[...]
Is there a more efficient way to do this than x -= (x % the_number)?

In the general case, no. There are alternatives with similar efficiency, such as

x = (x / the_number) * the_number

, but you're not going to do it with fewer than two arithmetic operations. (Also - is more efficient than * on some architectures, and / and % generally are about equivalent in efficiency).

If you want to truncate to a known-in-advance power of 2, however, then you can do it by masking off the lower-order bits with a single bitwise &. For instance, to truncate to the nearest lower multiple of 16 (== 0x10), you could write

x &= ~0xf;  // truncate an int x to a multiple of 16

Round a float to the nearest multiple of <n>?

It is easy

Math.round(value / n) * n


Related Topics



Leave a reply



Submit