How to check whether input value is integer or float?

前端 未结 8 1043
庸人自扰
庸人自扰 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: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");
    }
    

提交回复
热议问题