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.
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;
}
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
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;
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 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.
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 thanx -= (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 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;
Related Topics
Value-Initializing an Automatic Object
Why Do You Need to Append an L or F After a Value Assigned to a C++ Constant
Lambda Capture by Value Mutable Doesn't Work with Const &
How to Wrap a Function with Variable Length Arguments
Libstdc++ Glibcxx Version Errors
Why Have Unary_Function, Binary_Function Been Removed from C++11
Behaviour of Negative Zero (-0.0) in Comparison with Positive Zero (+0.0)
Check Whether Iterator Belongs to a List
Custom Stream to Method in C++
Determine How Many Times File Is Mapped into Memory
In C++, Is There a Difference Between "Throw" and "Throw Ex"
Partial Template Specialization Based on "Signed-Ness" of Integer Type
How to Debug C++11 Code with Unique_Ptr in Ddd (Or Gdb)
Two Library of Different Versions in an Application