2

When dividing by 1000 I sometime run across a bug that doesn't do the division "correctly". For example, when dividing 90.55 by 1000 instead of getting .09055 I get .090549999999.

<c:out value="${bean.paPrice / 1000}" />

Why is this happening? Is this a result of floating point math? A google search seemed to indicate that this may be a culprit, but I have found no concrete answer.

To fix this I can round the answer to 5 digits, but it seems like this is just a patch to a deeper problem.

<fmt:formatNumber type="number" maxFractionDigits="5" value="${bean.paPrice / 1000}" />
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JGaudette
  • 106
  • 1
  • 8
  • Duplicate: http://stackoverflow.com/questions/327544/strange-floating-point-behaviour-in-a-java-program – S.Lott Jun 16 '09 at 16:00
  • Also see http://stackoverflow.com/questions/322749/retain-precision-with-doubles-in-java – S.Lott Jun 16 '09 at 16:03

3 Answers3

3

It's not a problem, it's just a natural result of the binary representation of floating point. Round your values and don't worry about it.

Steve B.
  • 55,454
  • 12
  • 93
  • 132
2

Why do computers suck at math?

http://www.codinghorror.com/blog/archives/001266.html

sangretu
  • 1,208
  • 9
  • 12
1

Yes, this is a common floating-point format issue.

instanceof me
  • 38,520
  • 3
  • 31
  • 40