var a = new BigDecimal("0.0");
var b = new BigDecimal("0.05");
var result1 = a.add(b);
var c = new BigDecimal("0.00");
var result2 = c.add(b);
System.out.println(result1); // 0.05
System.out.println(result2); // 0.05
System.out.println(result1.equals(result2)); // true
System.out.println(result1.scale()); // 2
System.out.println(result2.scale()); // 2I'm surprised that a.add(b) promotes to an extra decimal place.
That's a touch ... odd.
> The BigDecimal class gives its user complete control over rounding behavior. If no rounding mode is specified and the exact result cannot be represented, an exception is thrown ...
- https://docs.oracle.com/javase/8/docs/api/java/math/BigDecim...
Apparently, restricted behavior like I expected is reserved for if you use a MathContext.
> Instead, for the BigDecimal operations taking a MathContext parameter, if the MathContext has a nonzero precision, the set of possible representable values for the result is determined by the precision of the MathContext argument.
Very strange. I would have expected something that is going to be mostly used for currency to not do implicit things by default.