Why does dividing JSP / JSTL by 1000 sometimes give a remainder?

When dividing by 1000, I someday encounter an error that does not make the division โ€œrightโ€. For example, dividing 90.55 by 1000 instead of getting .09055 I get .090549999999.

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

Why is this happening? Is this the result of floating point math? It seems that a Google search showed that this could be a criminal, but I did not find a specific answer.

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

<fmt:formatNumber type="number" maxFractionDigits="5" value="${bean.paPrice / 1000}" />
+2
source share
3 answers

This is not a problem, it is just a natural result of a binary floating-point representation. Summarize your values โ€‹โ€‹and don't worry about it.

+3

Yes, this is a common floating point format .

+1
source

All Articles