0

Possible Duplicate:
Java floating point math - (conversion for feet/meters)
Java floating point arithmetic

I am running into the following problem in java.

This computation (0.196f * 100) yields 19.599999487400055 instead of 19.6.

I am puzzled. Can anyone please tell me why I get this odd result and how I can make sure I get the proper result without having to pollute my code with rounding all over the place.

Also what other jvm language deals with such problems better? Would scala be any different for example?

Community
  • 1
  • 1
balteo
  • 23,602
  • 63
  • 219
  • 412

1 Answers1

0

As the comments already state, floating point numbers always suffer from inaccuracies. double suffers to a lesser extend but it still suffers from that, so you'll have to deal with them.

If you want exact calculations use BigDecimal although you can't use operators then.

Thomas
  • 87,414
  • 12
  • 119
  • 157