How to check whether input value is integer or float?

前端 未结 8 797
庸人自扰
庸人自扰 2020-12-13 05:44

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

相关标签:
8条回答
  • 2020-12-13 06:27

    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");
         }
    }
    
    0 讨论(0)
  • 2020-12-13 06:33

    Also:

    (value % 1) == 0
    

    would work!

    0 讨论(0)
提交回复
热议问题