如何测试双精度值是否为整数

有可能这样做吗?

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
}

我知道代码可能不是这样的,但确实是怎样的呢?

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

This checks if the rounded-down value of the double is the same as the double.

Your variable could have an int or double value and Math.floor(variable) always has an int value, so if your variable is equal to Math.floor(variable) then it must have an int value.

This also doesn't work if the value of the variable is infinite or negative infinite hence adding 'as long as the variable isn't inifinite' to the condition.

public static boolean isInt(double d)
{
return d == (int) d;
}
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);
}

Guava: DoubleMath.isMathematicalInteger. (Disclosure: I wrote it.) Or, if you aren't already importing Guava, x == Math.rint(x) is the fastest way to do it; rint is measurably faster than floor or ceil.

Or you could use the modulo operator:

(d % 1) == 0

you could try in this way: get the integer value of the double, subtract this from the original double value, define a rounding range and tests if the absolute number of the new double value(without the integer part) is larger or smaller than your defined range. if it is smaller you can intend it it is an integer value. Example:

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

If you assign to d the value 33.15 the method return true. To have better results you can assign lower values to testRange (as 0.0002) at your discretion.

Try this way,

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

for example:

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

hence 12.9 is not integer, nevertheless

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

hence 12.0 is integer

Here's a version for Integer and 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;
}
}

To convert Double to Integer:

Integer intVariable = variable.intValue();

Here's a solution:

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

Similar to SkonJeet's answer above, but the performance is better (at least in java):

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

Personally, I prefer the simple modulo operation solution in the accepted answer. Unfortunately, SonarQube doesn't like equality tests with floating points without setting a round precision. So we have tried to find a more compliant solution. Here it is:

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

Remainder(BigDecimal) returns a BigDecimal whose value is (this % divisor). If this one's equal to zero, we know there is no floating point.

Consider:

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

This sticks to core Java and avoids an equality comparison between floating point values (==) which is consdered bad. The isFinite() is necessary as rint() will pass-through infinity values.

Here is a good solution:

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

My simple solution:

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

Because of % operator cannot apply to BigDecimal and int (i.e. 1) directly, so I am using the following snippet to check if the BigDecimal is an integer:

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

A simple way for doing this could be

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

Similar (and probably inferior) to Eric Tan's answer (which checks scale):

double d = 4096.00000;
BigDecimal bd = BigDecimal.valueOf(d);
String s = bd.stripTrailingZeros().toPlainString();
      

boolean isInteger = s.indexOf(".")==-1;

My solution would be

double variable=the number;
if(variable-(int)variable=0.0){
// do stuff
}