I am trying to compare two double type variables that are rounded down to a specific decimal place using BigDecimals and RoundingMode.
For instance, when comparing 3.1456 and 3.145, rounded down to 3 decimal places in an if statement, my if statement should return true only if both values are equivalent to one another.
However, it did not work for my code
BigDecimal one = new BigDecimal(3.1456).setScale(3, RoundingMode.DOWN);
BigDecimal two = new BigDecimal(3.145).setScale(3, RoundingMode.DOWN);
if (one.equals(two)) {
System.out.println("Both variables are equal");
} else {
System.out.println("Both variables are not equal");
}
The output logic was false. So I did some checking to see the values that my variables are rounded down to. It was 3.145, and 3.144 respectively which was the unexpected one. Can't find out the reason why it's 3.144...
Next, I used another method to do the above by using DecimalFormat and RoundingMode. It worked pretty well.
DecimalFormat df0 = new DecimalFormat("#.###");
DecimalFormat df1 = new DecimalFormat("#.###");
df0.setRoundingMode(RoundingMode.DOWN);
df1.setRoundingMode(RoundingMode.DOWN);
if (df0.format(num0).equals(df1.format(num1))) {
System.out.println("Both variables are equal")
} else {
System.out.println("Both variables are not equal")
}
The method above works very well! BigDecimal evaluates both values to 3.145.
I just can't understand why it doesn't work when I use BigDecimal to do the task, compared to when I use DecimalFormat. Is there something to do with how double type variables work? Something like (double)(0.2-0.1) which gives a result of 0.09999999... and not 0.1?
Please enlighten me with a better solution and understanding! Thank you so much!