[英]How to test if a double is an integer
是否有可能做到这一点?
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
}
我知道代码可能不会去这样的事情,但它是如何走?
或者您可以使用模运算符:
(d % 1) == 0
if ((variable == Math.floor(variable)) && !Double.isInfinite(variable)) {
// integer type
}
这会检查双精度值的舍入值是否与双精度值相同。
你的变量可以有一个 int 或 double 值,而Math.floor(variable)
总是有一个 int 值,所以如果你的变量等于Math.floor(variable)
那么它必须有一个 int 值。
如果变量的值是无穷大或负无穷大,这也不起作用,因此将“只要变量不是无穷大”添加到条件中。
番石榴: DoubleMath.isMathematicalInteger
。 (披露:我写的。)或者,如果您还没有导入 Guava, x == Math.rint(x)
是最快的方法; rint
比floor
或ceil
快得多。
public static boolean isInt(double d)
{
return d == (int) d;
}
试试这个方法
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是整数
最好的方法是使用模数运算符
if(val % 1 == 0)
这是Integer
和Double
的一个版本:
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();
考虑:
Double.isFinite (value) && Double.compare (value, StrictMath.rint (value)) == 0
这坚持核心 Java 并避免浮点值 ( ==
) 之间的相等比较,这被认为是错误的。 isFinite()
是必要的,因为rint()
将传递无穷大值。
这是一个很好的解决方案:
if (variable == (int)variable) {
//logic
}
类似于上面 SkonJeet 的回答,但性能更好(至少在 java 中):
Double zero = 0d;
zero.longValue() == zero.doubleValue()
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);
}
你可以这样尝试:获取双精度值的整数值,从原始双精度值中减去它,定义一个舍入范围并测试新双精度值的绝对数(不含整数部分)是否大于或小于您的定义的范围。 如果它更小,你可以打算它是一个整数值。 例子:
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;
}
如果将值 33.15 分配给 d,则该方法返回 true。 为了获得更好的结果,您可以自行决定为 testRange 分配较低的值(如 0.0002)。
就个人而言,我更喜欢接受答案中的简单模运算解决方案。 不幸的是,SonarQube 不喜欢在没有设置舍入精度的情况下使用浮点数进行相等测试。 所以我们试图找到一个更合规的解决方案。 这里是:
if (new BigDecimal(decimalValue).remainder(new BigDecimal(1)).equals(BigDecimal.ZERO)) {
// no decimal places
} else {
// decimal places
}
Remainder(BigDecimal)
返回一个BigDecimal
其值为(this % divisor)
。 如果这个值等于 0,我们就知道没有浮点数。
我的简单解决方案:
private boolean checkIfInt(double value){
return value - Math.floor(value) == 0;
}
由于%
运算符不能直接应用于 BigDecimal 和 int(即 1),因此我使用以下代码段来检查 BigDecimal 是否为整数:
value.stripTrailingZeros().scale() <= 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";
与 Eric Tan 的答案(检查比例)相似(并且可能不如):
double d = 4096.00000;
BigDecimal bd = BigDecimal.valueOf(d);
String s = bd.stripTrailingZeros().toPlainString();
boolean isInteger = s.indexOf(".")==-1;
我的解决方案是
double variable=the number;
if(variable-(int)variable=0.0){
// do stuff
}
这是一个解决方案:
float var = Your_Value;
if ((var - Math.floor(var)) == 0.0f)
{
// var is an integer, so do stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.