0

some calculations with doubles return the wrong result.

E.g.

System.out.println(""+(0.05+0.01));

output

0.060000000000000005

What can I do to correct this error?

jmj
  • 237,923
  • 42
  • 401
  • 438
xxPMxx
  • 38
  • 3

1 Answers1

1

Doubles are not made for precise calculations (see Round to 2 decimal places) - for precise calculations, use BigDecimal instead.

Community
  • 1
  • 1
Smutje
  • 17,733
  • 4
  • 24
  • 41
  • BigDecimal only has a precision advantage for numbers with terminating decimal expansions. It is no better at representing one third than double. – Patricia Shanahan Jun 22 '14 at 10:19