Checking if a number is an Integer in Java

后端 未结 11 548
生来不讨喜
生来不讨喜 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:13

    Quick and dirty...

    if (x == (int)x)
    {
       ...
    }
    

    edit: This is assuming x is already in some other numeric form. If you're dealing with strings, look into Integer.parseInt.

提交回复
热议问题