8

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")
tplc10
  • 81
  • 1
  • 1
  • 3
  • as long as `i` is casted to be of type `Int` it will be a `Int` you can't divide a `Int` by a `Double`, etc --I should add I don't have a lot of experience with Kotlin, like one or two simple "projects" to play around with it a week or two ago, but that is the case for most all strongly typed languages. – Jacob Boyd Jul 31 '17 at 17:54

3 Answers3

16

An easy way to check whether a / b is a whole number is to check the remainder for being zero: a % b == 0.

Note, however, that if both operands of / are of an integral type (Short, Int, Long), then the division result is always an integer number (the fractional part is just dropped), so, if you have a val i: Int = 2 then i % 3 == 1 but i / 3 == 0. To use fractional division, make at least one of the operands fractional like i / 3.0 or i.toDouble / 3.

In case you want to check that a Double is whole, you can use d % 1.0 == 0.0 or check that Math.floor(d) == d.

hotkey
  • 140,743
  • 39
  • 371
  • 326
  • I read the question and the other answer three times and couldn't belief nobody was giving *this* answer. Was about to start typing when I saw your attempt ;-) – GhostCat Jul 31 '17 at 18:24
  • Is there any reason with this [code](https://pastebin.com/PmhnsHhX), the second i variable has an unresolved reference error. How would I fix this? – tplc10 Jul 31 '17 at 19:06
  • 1
    In Kotlin I would prefer to write `a.rem(b)` instead of `a % b`. This makes code more readable – Dmytro Rostopira Jul 31 '17 at 20:05
  • 1
    `d.compareTo(d.toInt()) == 0` gives incorrect result if `d` is outside of `Int` range. – Ilya Aug 01 '17 at 01:40
  • 1
    Thanks for `d % 1.0 == 0.0`. But better is `Math.abs(d) % 1.0 < 1e-10`. – CoolMind Nov 29 '19 at 13:44
8

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)")
}
Mohamed Medhat
  • 761
  • 11
  • 16
1

is operator in Kotlin is used to check the instance of the object.

Here you have written (i / 3) is Int means:

You are checking (i / 3) is an instance of Int or not.

In Kotlin, you have to write the below code to check a number is a whole number or not.

If i is an int

if((i / 3).toInt() == (i / 3)) {
    print("Whole Number")
}

otherwise you can also use the below code:

if((i / 3).toInt().compareTo(i / 3) == 0) {
    print("Whole Number")
}
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
  • 1
    `(i / 3).toInt() == (i / 3)` is always true if `i` is an `Int`, and it's invalid code if it's not. – hotkey Jul 31 '17 at 18:07