My task is to compare two numbers and return true if they are equal by three decimal places. When I print out the value of diff it is 0.0001, but it still doesn't enter the if(diff <= 0.0001) block.
public class DecimalComparator {
public static boolean areEqualByThreeDecimalPlaces(double num1, double num2) {
double diff = Math.abs(num1 - num2);
System.out.printf("%f", diff);
if(diff <= 0.0001) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
boolean test = areEqualByThreeDecimalPlaces(3.1756, 3.1755);
System.out.println(test);
test = areEqualByThreeDecimalPlaces(3.176, 3.175);
System.out.println(test);
}
}