BigDecimal's equals()
method compares scale too, so
new BigDecimal("0.2").equals(new BigDecimal("0.20")) // false
It's contested why it behaves like that.
Now, suppose I have a Set<BigDecimal>
, how do I check if 0.2 is in that Set, scale independent?
Set<BigDecimal> set = new HashSet<>();
set.add(new BigDecimal("0.20"));
...
if (set.contains(new BigDecimal("0.2")) { // Returns false, but should return true
...
}