Returning the nearest multiple value of a number
Some division and rounding should be all you need for this:
int value = 30;
int factor = 16;
int nearestMultiple =
(int)Math.Round(
(value / (double)factor),
MidpointRounding.AwayFromZero
) * factor;
Be careful using this technique. The Math.Round(double)
overload believes the evil mutant MidpointRounding.ToEven
is the best default behavior, even though what we all learned before in school is what the CLR calls MidpointRounding.AwayFromZero
. For example:
var x = Math.Round(1.5); // x is 2.0, like you'd expect
x = Math.Round(0.5); // x is 0. WAT?!
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 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;
How can I round down to the nearest multiple of 3 in PHP?
Is this what you looking for.
/**
* Round an integer down to the nearest multiple of the given multiple
* @param integer $number
* @param integer $multiple
* @return integer
*/
function round_down_to_multiple_of( int $number, int $multiple = 3 ): int
{
return (int)($multiple * floor( $number/$multiple ));
}
# TESTS
$numbers = [ 10, 9, 8, 1, 0 ];
foreach( $numbers as $number ){
printf( '%d became %d'.PHP_EOL, $number, round_down_to_multiple_of( $number, 3 ) );
}
After running the above test I get the following results:
10 became 9
9 became 9
8 became 6
1 became 0
0 became 0
Round a float to the nearest multiple of n?
It is easy
Math.round(value / n) * n
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
Related Topics
Hide Form Instead of Closing When Close Button Clicked
How to Check If Ioexception Is Not-Enough-Disk-Space-Exception Type
How to Define Multiple Names for Xmlelement Field
Automatically Rename a File If It Already Exists in Windows Way
C# Waiting for Multiple Threads to Finish
Mongodb .Net Driver 2.0 Pull (Remove Element)
How Should You Diagnose the Error Sehexception - External Component Has Thrown an Exception
.Net Application Cannot Start and Receive Xamlparseexception
How to Check If Another Instance of the Application Is Running
Getting the Index of a Particular Item in Array