Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 854
user1541106 Avatar asked Aug 23 '12 18:08

user1541106


1 Answers

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

public boolean isEvenlyDivisable(int a, int b) {
    return a % b == 0;
}
like image 85
Will Hartung Avatar answered Oct 30 '22 02:10

Will Hartung