public class DoubleIntegerLikeCheck {
// -------------------------------------------------------------------------
// Determines whether a double represents an integer value.
// Uses a small tolerance to handle floating‑point precision issues.
// -------------------------------------------------------------------------
public static boolean isInteger(double value) {
final double epsilon = 1e-12;
// Casting to long truncates toward zero; compare with tolerance.
long truncated = (long) value;
// If the absolute difference is extremely small, treat it as an integer.
return Math.abs(value - truncated) < epsilon;
}
// -------------------------------------------------------------------------
// Prints a descriptive message for the given double.
// -------------------------------------------------------------------------
public static void printCheck(double value) {
System.out.println("Checking value: " + value);
if (isInteger(value)) {
System.out.println("Result: The value contains an integer.\n");
} else {
System.out.println("Result: The value contains a floating-point number.\n");
}
}
// -------------------------------------------------------------------------
// Entry point.
// -------------------------------------------------------------------------
public static void main(String[] args) {
// Example input values
double a = 42.0; // integer-like
double b = 42.00000000001; // floating-point due to tiny fraction
double c = 89.0000000000001; // integer-like
double d = -7.0; // negative integer-like
double e = 3.14; // floating-point
// Run checks
printCheck(a);
printCheck(b);
printCheck(c);
printCheck(d);
printCheck(e);
}
}
/*
run:
Checking value: 42.0
Result: The value contains an integer.
Checking value: 42.00000000001
Result: The value contains a floating-point number.
Checking value: 89.0000000000001
Result: The value contains an integer.
Checking value: -7.0
Result: The value contains an integer.
Checking value: 3.14
Result: The value contains a floating-point number.
*/