为什么JSP / JSTL除以1000有时会给余数呢? [英] Why does JSP/JSTL division by 1000 sometimes give remainder?

查看:355
本文介绍了为什么JSP / JSTL除以1000有时会给余数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除以1000,我有时会碰到一个不能正确执行分割的错误。例如,将90.55除以1000而不是得到.09055时,得到.090549999999。

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

为什么会发生这种情况?这是浮点数学的结果吗?谷歌搜索似乎表明,这可能是一个罪魁祸首,但我没有找到具体的答案。

为了解决这个问题,我可以围绕5位数的答案,但它似乎这只是一个更深层次的问题。

 < fmt:formatNumber type =numbermaxFractionDigits =5 value =$ {bean.paPrice / 1000}/> 


解决方案

这不是问题,它只是一个自然的结果浮点的二进制表示。圆你的价值,不要担心。


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}" />

解决方案

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.

这篇关于为什么JSP / JSTL除以1000有时会给余数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆