63

How to check whether input value is integer or float?

Suppose 312/100=3.12 Here i need check whether 3.12 is a float value or integer value, i.e., without any decimal place value.

Ojasv singh
  • 474
  • 8
  • 19
user569125
  • 1,423
  • 13
  • 29
  • 40
  • please define float and integet in your words. I didn't downvote though – jmj Jan 18 '11 at 18:22
  • We need more information to be able to help you. Some types would help. For example, the 3.12 you provide above. Is that coming in as a String? Or, are you dividing 2 ints and want to know if the answer is an int? Help us help you. – rfeak Jan 18 '11 at 18:22
  • Hi,Why down vote for java freshers.both are int values.I am looking any predefined method to check the result value is either floating or int. – user569125 Jan 18 '11 at 18:27
  • It's not clear from the question whether you want to check whether the resulting value itself is an integer or the variable that holds it is. – Ariel Jan 18 '11 at 18:27
  • i have to check on result value. – user569125 Jan 18 '11 at 18:30

8 Answers8

53

You should check that fractional part of the number is 0. Use

x==Math.ceil(x)

or

x==Math.round(x)

or something like that

Alex
  • 2,438
  • 23
  • 30
  • 10
    Casting, `((int) x) == x`, is fastest. `x == Math.round(x)` is faster than `x == Math.ceil(x)`. Modulus `x % 1 == 0` is slowest. – Ahmet Oct 26 '19 at 21:29
  • Regardless of https://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java ? – Thomas Weller Nov 08 '20 at 17:52
23

How about this. using the modulo operator

if(a%b==0) 
{
    System.out.println("b is a factor of a. i.e. the result of a/b is going to be an integer");
}
else
{
    System.out.println("b is NOT a factor of a");
}
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • +1 This seems to be the best solution. If you divide two really big numbers, you can't tell for sure if the result is really an int, or just very close to it, so the approaches with ceil, floor or round might fail. – Landei Jan 18 '11 at 19:30
  • I agree but since the OP had already accepted the answer, i didn't bother to respone :-(. Also this is the commonsense way of doing it and hence intuitive and simple to maintain – Aravind Yarram Jan 18 '11 at 19:35
17

The ceil and floor methods will help you determine if the number is a whole number.

However if you want to determine if the number can be represented by an int value.

if(value == (int) value)

or a long (64-bit integer)

if(value == (long) value)

or can be safely represented by a float without a loss of precision

if(value == (float) value)

BTW: don't use a 32-bit float unless you have to. In 99% of cases a 64-bit double is a better choice.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • in your three examples, what is the type for the variable `value`? – broc.seib Dec 20 '18 at 15:51
  • 1
    @broc.seib any numeric primitive, `byte`, `char`, `int`, `short`, `float`, `double` or `long` would be fine however the first check only makes sense for `long`, `float` or `double` otherwise it is always true. – Peter Lawrey Dec 24 '18 at 19:06
16

Also:

(value % 1) == 0

would work!

Farshid Zaker
  • 1,960
  • 2
  • 22
  • 39
15

Math.round() returns the nearest integer to your given input value. If your float already has an integer value the "nearest" integer will be that same value, so all you need to do is check whether Math.round() changes the value or not:

if (value == Math.round(value)) {
  System.out.println("Integer");
} else {
  System.out.println("Not an integer");
}
Herohtar
  • 5,347
  • 4
  • 31
  • 41
5

You can use Scanner Class to find whether a given number could be read as Int or Float type.

 import java.util.Scanner;
 public class Test {
     public static void main(String args[] ) throws Exception {

     Scanner sc=new Scanner(System.in);

     if(sc.hasNextInt())
         System.out.println("This input is  of type Integer");
     else if(sc.hasNextFloat())
         System.out.println("This input is  of type Float");
     else
         System.out.println("This is something else");
     }
}
artem
  • 46,476
  • 8
  • 74
  • 78
Lalit kumar
  • 382
  • 4
  • 13
3

Do this to distinguish that.

If for example your number is 3.1214 and stored in num but you don't know kind of num:

num = 3.1214
// cast num to int
int x = (int)num;
if(x == num)
{
  // num is a integer
} 
else
  // num is float
}

In this example we see that num is not integer.

Gary
  • 13,303
  • 18
  • 49
  • 71
Mahdi_Nine
  • 14,205
  • 26
  • 82
  • 117
0

You can use RoundingMode.#UNNECESSARY if you want/accept exception thrown otherwise

new BigDecimal(value).setScale(2, RoundingMode.UNNECESSARY);

If this rounding mode is specified on an operation that yields an inexact result, an ArithmeticException is thrown.

Exception if not integer value:

java.lang.ArithmeticException: Rounding necessary
Ori Marko
  • 56,308
  • 23
  • 131
  • 233