How to test if a double is an integer

JavaMath

Java Problem Overview


Is it possible to do this?

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
}

I know the code probably doesn't go anything like that, but how does it go?

Java Solutions


Solution 1 - Java

Or you could use the modulo operator:

(d % 1) == 0

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

Here is a good solution:

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

Solution 7 - Java

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.

Solution 8 - Java

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

Solution 9 - Java

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

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

Solution 10 - Java

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

Solution 11 - Java

My simple solution:

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

Solution 12 - Java

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

Solution 13 - Java

My solution would be

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

Solution 14 - Java

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.

Solution 15 - Java

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.

Solution 16 - Java

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

Solution 17 - Java

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;

Solution 18 - Java

Here's a solution:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJXPheonixView Question on Stackoverflow
Solution 1 - JavaSkonJeetView Answer on Stackoverflow
Solution 2 - JavaMax HudsonView Answer on Stackoverflow
Solution 3 - JavaLouis WassermanView Answer on Stackoverflow
Solution 4 - JavaEng.FouadView Answer on Stackoverflow
Solution 5 - JavaSheldonView Answer on Stackoverflow
Solution 6 - JavaNitishView Answer on Stackoverflow
Solution 7 - Javasimon.wattsView Answer on Stackoverflow
Solution 8 - JavairudyakView Answer on Stackoverflow
Solution 9 - JavaedwardsayerView Answer on Stackoverflow
Solution 10 - JavamaericsView Answer on Stackoverflow
Solution 11 - JavaMostafa AmerView Answer on Stackoverflow
Solution 12 - JavaAnupriya_TechView Answer on Stackoverflow
Solution 13 - Javadfmaaa1View Answer on Stackoverflow
Solution 14 - JavaSalvi94View Answer on Stackoverflow
Solution 15 - JavachaeschuechliView Answer on Stackoverflow
Solution 16 - JavaEric TanView Answer on Stackoverflow
Solution 17 - JavaPaulView Answer on Stackoverflow
Solution 18 - JavaMOHIT GUPTAView Answer on Stackoverflow