How is it that Java\'s BigDecimal
can be this painful?
Double d = 13.3D;
BigDecimal bd1 = new BigDecimal(d);
BigDecimal bd2 = new BigDecimal(St
Your problem has nothing to do with BigDecimal
, and everything with Double
, which cannot represent 13.3 accurately, since it uses binary fractions internally.
So your error is introduced in the very first line. The first BigDecimal
simply preserves it, while String.valueOf()
does some fishy rounding that causes the second one to have the desired content, pretty much through luck.