java float 判断整数_java - 如何测试double是否为整数

java - 如何测试double是否为整数

是否有可能做到这一点?

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

}

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

JXPheonix asked 2019-05-28T19:00:07Z

12个解决方案

174 votes

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

(d % 1) == 0

SkonJeet answered 2019-05-28T19:01:07Z

107 votes

if ((variable == Math.floor(variable)) && !Double.isInfinite(variable)) {

// integer type

}

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

您的变量可以具有int或double值,并且Math.floor(variable)始终具有int值,因此如果您的变量等于Math.floor(variable),则它必须具有int值。

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

maxhud answered 2019-05-28T19:00:34Z

72 votes

番石榴:x == Math.rint(x)。(披露:我写的。)或者,如果你还没有进口番石榴,x == Math.rint(x)是最快的方法; rint比floor或ceil快得多。

Louis Wasserman answered 2019-05-28T19:01:33Z

17 votes

public static boolean isInt(double d)

{

return d == (int) d;

}

Eng.Fouad answered 2019-05-28T19:01:51Z

4 votes

试试这个,

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是整数

Sheldon answered 2019-05-28T19:02:43Z

2 votes

考虑:

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

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

simon.watts answered 2019-05-28T19:03:10Z

0 votes

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

}

maerics answered 2019-05-28T19:03:29Z

0 votes

您可以尝试这种方式:获取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)。

Salvi94 answered 2019-05-28T19:04:03Z

0 votes

这是Double和Integer的版本:

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();

irudyak answered 2019-05-28T19:04:36Z

0 votes

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

Double zero = 0d;

zero.longValue() == zero.doubleValue()

edwardsayer answered 2019-05-28T19:05:02Z

0 votes

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

if (new BigDecimal(decimalValue).remainder(new BigDecimal(1)).equals(BigDecimal.ZERO)) {

// no decimal places

} else {

// decimal places

}

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

chaeschuechli answered 2019-05-28T19:05:37Z

-1 votes

这是一个解决方案:

float var = Your_Value;

if ((var - Math.floor(var)) == 0.0f)

{

// var is an integer, so do stuff

}

MOHIT GUPTA answered 2019-05-28T19:06:00Z


版权声明:本文为weixin_42562079原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。