BigDecimal 2.0 is coming equal to BigDecimal 2.00 [duplicate]
At compile time the double
s 2.0
and 2.00
are the same. You need a String
. Like,
System.out.println(new BigDecimal(2.0).equals(new BigDecimal("2.00")));
Outputs
false
Your current code is the equivalent of
System.out.println(2 == 2.00);
Which is obviously true
.
To expand on that last example, consider
BigDecimal a = new BigDecimal(2.0);
BigDecimal b = a.setScale(2);
System.out.printf("%s equals %s is %s%n", a, b, a.equals(b));
which should explain why your current code behaves as it does; because
2 equals 2.00 is false