What you actually want is
new BigDecimal("0.1")
.add(new BigDecimal("0.1"))
.add(new BigDecimal("0.1"));
The new BigDecimal(double)
constructor gets all the imprecision of the double
, so by the time you’ve said 0.1
, you’ve already introduced the rounding error. Using the String
constructor avoids the rounding error associated with going via the double
.