Checking if the output of a calculation is a whole number

Viewed 7749

What is a method to check if the output of a calculation is a whole number? I've tried doing this:

if ((i / 3) is Int ) {
print("Whole Number")
}

But it seems to be checking the type of the variable, not what the output is.


Edit: Apparently if the variable is an integer, it automatically rounds the output of the operation, so I had to do something like this:

 if((i.toFloat()/3) == (i / 3).toFloat()){
        println("Whole Number")
3 Answers

A simple trick is to check if the values of ceil and floor functions are equal or not

If they are equal, the result is a whole number
if not, the result is not a whole number

if (ceil(i/3) == floor(i/3)) {
    print("int (a whole number)")
} else {
    print("not int (not a whole number)")
}
Related