简体   繁体   English

如何检查输入值是整数还是浮点数?

[英]How to check whether input value is integer or float?

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, ie, without any decimal place value.假设 312/100=3.12 这里我需要检查 3.12 是浮点值还是整数值,即没有任何小数位值。

You should check that fractional part of the number is 0. Use您应该检查数字的小数部分是否为 0。使用

x==Math.ceil(x)

or要么

x==Math.round(x)

or something like that或类似的东西

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");
}

The ceil and floor methods will help you determine if the number is a whole number. ceil 和 floor 方法将帮助您确定数字是否为整数。

However if you want to determine if the number can be represented by an int value.但是,如果要确定数字是否可以用 int 值表示。

if(value == (int) value)

or a long (64-bit integer)或 long(64 位整数)

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.顺便说一句:除非必须,否则不要使用 32 位浮点数。 In 99% of cases a 64-bit double is a better choice.在 99% 的情况下,64 位 double 是更好的选择。

Also:还:

(value % 1) == 0

would work!会工作!

Math.round() returns the nearest integer to your given input value. Math.round()返回最接近给定输入值的整数。 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:如果您的浮点数已经有一个整数值,那么“最近的”整数将是相同的值,因此您需要做的就是检查Math.round()是否更改了该值:

if (value == Math.round(value)) {
  System.out.println("Integer");
} else {
  System.out.println("Not an integer");
}

You can use Scanner Class to find whether a given number could be read as Int or Float type.您可以使用 Scanner Class 来查找给定数字是否可以读取为 Int 或 Float 类型。

 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");
     }
}

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:例如,如果您的号码是 3.1214 并存储在 num 中,但您不知道 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.在这个例子中,我们看到 num 不是整数。

You can use RoundingMode.#UNNECESSARY if you want/accept exception thrown otherwise如果您想要/接受否则抛出的异常,您可以使用RoundingMode.#UNNECESSARY

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.如果在产生不精确结果的操作上指定此舍入模式,则会引发 ArithmeticException。

Exception if not integer value:如果不是整数值则异常:

java.lang.ArithmeticException: Rounding necessary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM