Surprising behaviour

What result do you expect from this code?

BigDecimal a = new BigDecimal("2.00");
BigDecimal b = new BigDecimal("2.0");
System.out.println( a.equals(b) );

It does not look suspicious and you would suspect it to simply render true, right?

Wrong!

Unfortunately, this prints out a big red false.

JavaDoc to the rescue

As you may read in the JavaDoc:

BigDecimal JavaDoc for the 'equals' method

Mystery solved

Oh, so scale needs to be the same as well. How intuitive!

Solution

If what you really wanted is to compare BigDecimals as values and not as objects you should have used compareTo method:

BigDecimal a = new BigDecimal("2.00");
BigDecimal b = new BigDecimal("2.0");
System.out.println( a.compareTo(b) == 0 ); // true

TLDR;

When comparing BigDecimals use compareTo instead of equals.