Max Kortge
Max Kortge

Reputation: 527

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

Upvotes: 4

Views: 3906

Answers (3)

xenteros
xenteros

Reputation: 15842

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;

Upvotes: 10

Andreas
Andreas

Reputation: 159114

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

Upvotes: 8

Yassin Hajaj
Yassin Hajaj

Reputation: 21975

The easiest way I can think of is the following

public static int round(int num) {
    return num - num % 45 + (num%45==0? 0 : 45);
}

Tested

int num1 = 67;
int num2 = 43;
int num3 = 90;

System.out.println(num1 - num1 % 45 + (num1%45==0? 0 : 45)); // 90
System.out.println(num2 - num2 % 45 + (num2%45==0? 0 : 45)); // 45
System.out.println(num3 - num3 % 45 + (num3%45==0? 0 : 45)); // 90

Upvotes: 2

Related Questions