4

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
xenteros
  • 14,275
  • 12
  • 47
  • 81
Max Kortge
  • 515
  • 6
  • 23

3 Answers3

9

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;
xenteros
  • 14,275
  • 12
  • 47
  • 81
  • Could shorten it using conditional operator `return (temp == 0 ? num : num + base - temp);` and method should be `static`. Also be aware that it cannot handle negative values. Still, up-voted. – Andreas Jun 30 '16 at 08:27
  • 1
    I agree with negatives. I don't use conditional operator when explaining stuff to beginners as it's not that easy for them. At the compilation they're both the same and for reading my version is easier to newbies :) Thanks for upvoting – xenteros Jun 30 '16 at 08:30
  • 3
    Then for newbies, you shouldn't put `return` on same line as `if`. ;-) Hmmm.... I do guess that my answer doesn't really help newbies understand the intricacies of integer math, huh. – Andreas Jun 30 '16 at 08:31
7

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
Andreas
  • 138,167
  • 8
  • 112
  • 195
2

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
Yassin Hajaj
  • 20,020
  • 9
  • 41
  • 81