Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've looked at many questions regarding rounding up to the nearest multiple of a number, but I can't understand their methods well enough to adopt them for rounding up to 45 or they use language specific methods of other languages.

Here is a bit more detailed explanation if the above doesn't make much sense yet:

Example input:

int num1 = 67;
int num2 = 43;

Expected results:

>>round(num1) = 90
>>round(num2) = 45
like image 974
Max Kortge Avatar asked Dec 02 '22 12:12

Max Kortge


2 Answers

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;
like image 164
xenteros Avatar answered Dec 05 '22 09:12

xenteros


Since people have such trouble with rounding to multiple of an integer number, whether rounding up/down/nearest, here are very simple methods for doing so:

public static int roundDown(int value, int multiplier) {
    return value / multiplier * multiplier;
}
public static int roundHalfUp(int value, int multiplier) {
    return (value + multiplier / 2) / multiplier * multiplier;
}
public static int roundUp(int value, int multiplier) {
    return (value + multiplier - 1) / multiplier * multiplier;
}

Test

Value Down Half   Up
    0    0    0    0
    1    0    0    4
    2    0    4    4
    3    0    4    4
    4    4    4    4
Value Down Half   Up
    0    0    0    0
    1    0    0    5
    2    0    0    5
    3    0    5    5
    4    0    5    5
    5    5    5    5

Negative Numbers

They don't work right for negative numbers. "Round down" usually means "towards zero", unlike Math.floor() which rounds towards negative infinity.

Here are versions that can handle negative values. These are consistent with the RoundingMode options of similar names used by BigDecimal.

public static int roundDown(int value, int multiplier) {
    if (multiplier <= 0) throw new IllegalArgumentException();
    return value / multiplier * multiplier;
}
public static int roundHalfUp(int value, int multiplier) {
    if (multiplier <= 0) throw new IllegalArgumentException();
    return (value + (value < 0 ? multiplier / -2 : multiplier / 2)) / multiplier * multiplier;
}
public static int roundUp(int value, int multiplier) {
    if (multiplier <= 0) throw new IllegalArgumentException();
    return (value + (value < 0 ? 1 - multiplier : multiplier - 1)) / multiplier * multiplier;
}

Test

Value Down Half   Up
   -4   -4   -4   -4
   -3    0   -4   -4
   -2    0   -4   -4
   -1    0    0   -4
    0    0    0    0
    1    0    0    4
    2    0    4    4
    3    0    4    4
    4    4    4    4
Value Down Half   Up
   -5   -5   -5   -5
   -4    0   -5   -5
   -3    0   -5   -5
   -2    0    0   -5
   -1    0    0   -5
    0    0    0    0
    1    0    0    5
    2    0    0    5
    3    0    5    5
    4    0    5    5
    5    5    5    5
like image 40
Andreas Avatar answered Dec 05 '22 07:12

Andreas