如何测试double是否为整数

时间:2012-03-27 22:17:26

标签: java math

是否可以这样做?

double variable;
variable = 5;
/* the below should return true, since 5 is an int. 
if variable were to equal 5.7, then it would return false. */
if(variable == int) {
    //do stuff
}

我知道代码可能不会那样,但是 它是怎么回事?

19 个答案:

答案 0 :(得分:182)

或者您可以使用模运算符:

(d % 1) == 0

答案 1 :(得分:117)

if ((variable == Math.floor(variable)) && !Double.isInfinite(variable)) {
    // integer type
}

检查double的向下舍入值是否与double相同。

你的变量可以有一个int或double值,Math.floor(variable)总是有一个int值,所以如果你的变量等于Math.floor(variable)那么它必须有一个int值。

如果变量的值是无穷大或负无穷大,这也不起作用,因此在条件中添加“只要变量不是无限的”。

答案 2 :(得分:78)

番石榴:DoubleMath.isMathematicalInteger。 (披露:我写过它。)或者,如果你还没有导入番石榴,x == Math.rint(x)是最快的方法; rint明显快于floorceil

答案 3 :(得分:18)

public static boolean isInt(double d)
{
    return d == (int) d;
}

答案 4 :(得分:4)

试试这种方式,

public static boolean isInteger(double number){
    return Math.ceil(number) == Math.floor(number); 
}

例如:

Math.ceil(12.9) = 13; Math.floor(12.9) = 12;

因此 12.9 整数,不过

 Math.ceil(12.0) = 12; Math.floor(12.0) =12; 

因此 12.0 是整数

答案 5 :(得分:2)

与上面的SkonJeet的答案类似,但性能更好(至少在java中):

Double zero = 0d;    
zero.longValue() == zero.doubleValue()

答案 6 :(得分:2)

考虑:

Double.isFinite (value) && Double.compare (value, StrictMath.rint (value)) == 0

这坚持核心Java,并避免浮点值(==)之间的相等比较,这是不好的。 isFinite()是必要的,因为rint()将传递无穷大值。

答案 7 :(得分:1)

以下是IntegerDouble的版本:

    private static boolean isInteger(Double variable) {
    if (    variable.equals(Math.floor(variable)) && 
            !Double.isInfinite(variable)          &&
            !Double.isNaN(variable)               &&
            variable <= Integer.MAX_VALUE         &&
            variable >= Integer.MIN_VALUE) {
        return true;
    } else {
        return false;
    }
}

Double转换为Integer

Integer intVariable = variable.intValue();

答案 8 :(得分:1)

这是一个很好的解决方案:

if ((bool)(variable == (int)variable)) {
    //logic
}

答案 9 :(得分:0)

您可以尝试这种方式:获取double的整数值,从原始double值中减去它,定义舍入范围并测试新double值的绝对数量(没有整数部分)是否更大或小于您定义的范围。如果它更小,你可以打算它是一个整数值。例如:

public final double testRange = 0.2;

public static boolean doubleIsInteger(double d){
    int i = (int)d;
    double abs = Math.abs(d-i);
    return abs <= testRange;
}

如果为d赋值33.15,则方法返回true。要获得更好的结果,您可以自行决定将较低的值分配给testRange(为0.0002)。

答案 10 :(得分:0)

就个人而言,我更喜欢简单的模运算解决方案。 不幸的是,SonarQube不喜欢浮点数的等式测试而没有设置圆精度。所以我们试图找到一个更合规的解决方案。这是:

if (new BigDecimal(decimalValue).remainder(new BigDecimal(1)).equals(BigDecimal.ZERO)) {
    // no decimal places
} else {
    // decimal places
}

Remainder(BigDecimal)返回BigDecimal,其值为(this % divisor)。如果这个等于零,我们知道没有浮点。

答案 11 :(得分:0)

public static boolean isInteger(double d) {
  // Note that Double.NaN is not equal to anything, even itself.
  return (d == Math.floor(d)) && !Double.isInfinite(d);
}

答案 12 :(得分:0)

最好的方法是使用模运算符

 if(val % 1 == 0)

答案 13 :(得分:0)

我的简单解决方案:

private boolean checkIfInt(double 
 value){
 return value - Math.floor(value) == 0;
 }

答案 14 :(得分:0)

由于%运算符无法直接应用于BigDecimal和int(即1),因此我正在使用以下代码段检查BigDecimal是否为整数:

value.stripTrailingZeros().scale() <= 0

答案 15 :(得分:0)

这样做的一个简单方法是

    double d = 7.88;    //sample example
    int x=floor(d);     //floor of number
    int y=ceil(d);      //ceil of number
    if(x==y)            //both floor and ceil will be same for integer number
        cout<<"integer number";
    else
        cout<<"double number";

答案 16 :(得分:0)

与 Eric Tan 的回答(检查比例)相似(并且可能不如):

double d = 4096.00000;
BigDecimal bd = BigDecimal.valueOf(d);
String s = bd.stripTrailingZeros().toPlainString();
      
boolean isInteger = s.indexOf(".")==-1;

答案 17 :(得分:0)

我的解决方案是

HttpResponseMessage responsePageFirst = await clientPageFirst.GetAsync(fullURL);

答案 18 :(得分:-1)

这是一个解决方案:

float var = Your_Value;
if ((var - Math.floor(var)) == 0.0f)
{
    // var is an integer, so do stuff
}