Warning: would you consider 0.9999999999999999
an integer? Probably not. But watch this:
double val = 0;
for(int i=0;i<10;i++)
System.out.println(val+=0.1);
This prints out:
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
Which is problematic because at the end of the program val
contains something that you think should be integer but is not.
So I'd make my method a bit slower (but more correct) by integrating a delta like JUnit does:
private static boolean isInt(double x, double delta) {
double ceil = Math.ceil(x);
return x-delta<ceil && x+delta>ceil;
}
and of course provide the same method with a sensible default:
private static boolean isInt(double x) {
return isInt(x, 0.000000001);
}
Now isInt(val)
finally returns true.