MaxKrissigo MaxKrissigo - 5 months ago 7
Java Question

Rounding an integer up to multiples of 45 in Java

I've looked at many questions regarding rounding up to the nearest multiple of a number, but I can't seem to get their methods to fit with rounding up to 45 or they are in a different language. Here is a bit more of a detailed explanation if it doesn't make much sense:

int num1 = 67;
int num2 = 43;

//Expected results
>>round(num1) = 90
>>round(num2) = 45

Answer

It's enough to add the missing mod45:

int upperround45(int i) {
    int temp = i%45;
    if (temp == 0) {
        return i;
    } else {
        return i + 45 - temp;
    }
}