Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a number is an Integer in Java

Tags:

java

utilities

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 I didn't find any method that will help me with this.

Where should I check? Integer Api?

like image 248
Unknown user Avatar asked Mar 31 '11 15:03

Unknown user


People also ask

How do you check a number is integer or not in Java?

To check if a String contains digit character which represent an integer, you can use Integer. parseInt() . To check if a double contains a value which can be an integer, you can use Math. floor() or Math.

How do you check if a number is an integer?

The Number. isInteger() method returns true if a value is an integer of the datatype Number. Otherwise it returns false .


1 Answers

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.

like image 55
Bleaourgh Avatar answered Sep 19 '22 01:09

Bleaourgh