Java - How to check if a division is an integer or a float?

user1541106 picture user1541106 · Aug 23, 2012 · Viewed 13.8k times · Source

Couldnt think of a better title. Well the problem is: I have the "int i", it can be any value. my goal is to transform the "int i" to the closest number divisible by 16.

For example, I got i = 33. Then i will be turned to 32 (16x2).But if I get i = 50, then it will be turned to 48 (16x3).

I tried many things for example:

for (int x = i; x < 999; x++){
if ( (i - x)/16 *is an integer*){
i = i - x;
}

But I dont know how to check if its an integer. So maybe my previous code work, but I just need to find a way to check if its an integer or a float. So.. any help is appreciated.

Answer

Will Hartung picture Will Hartung · Aug 23, 2012

Use the mod operator. Mod gives you the remainder of a division operation.

public boolean isEvenlyDivisable(int a, int b) {
    return a % b == 0;
}