When BigDecimal
is used with an input of double
and BigDecimal
with an input of String
different results seem to appear.
BigDecimal a = new BigDecimal(0.333333333);
BigDecimal b = new BigDecimal(0.666666666);
BigDecimal c = new BigDecimal("0.333333333");
BigDecimal d = new BigDecimal("0.666666666");
BigDecimal x = a.multiply(b);
BigDecimal y = c.multiply(d);
System.out.println(x);
System.out.println(y);
x outputs as
0.222222221777777790569747304508155316795087227497352441864147715340493949298661391367204487323760986328125
while y is
0.222222221777777778
Am I wrong in saying that this is because of double imprecision? But since this is a BigDecimal
, shouldn't it be the same?
double
has limited precision; That's IEEE 754 for you. – Overlarge.setScale(10, BigDecimal.ROUND_HALF_UP);
– Zenaidazenana