Checking if a number is an Integer in Java

后端 未结 11 557
生来不讨喜
生来不讨喜 2020-12-09 15:48

Is there any method or quick way to check whether a number is an Integer (belongs to Z field) in Java?

I thought of maybe subtracting it from the rounded number, but

11条回答
  •  一个人的身影
    2020-12-09 16:12

    One example more :)

    double a = 1.00
    
    if(floor(a) == a) {
       // a is an integer
    } else {
       //a is not an integer.
    }
    

    In this example, ceil can be used and have the exact same effect.

提交回复
热议问题