Comparing numbers by three decimal points

95 Views Asked by At
public class DecimalComparator {
    public static void main(String[] args) {
        System.out.println(areEqualByThreeDecimalPlaces(3.174 , 3.175));
        System.out.println(areEqualByThreeDecimalPlaces(3.175 , 3.176));
    }
    public static boolean areEqualByThreeDecimalPlaces(double firstNumber, double secondNumber){
        return (firstNumber >= secondNumber) && ((firstNumber - secondNumber) < 0.001) ? true : (firstNumber < secondNumber) && ((secondNumber - firstNumber) < 0.001) ? true : false;
    }
}

I was expecting false on both numbers but got true, false instead. This was the only test which my method didn't pass.

As I said before my test passed 19 other values and couldn't pass this one. Even the examples that I gave are really close.

0

There are 0 best solutions below