由买买提看人间百态

topics

全部话题 - 话题: 319499
(共0页)
o*********r
发帖数: 203
1
来自主题: Programming版 - 【急】JS round 运算出错如何破
I think that it's something to do with the IEEE floating point standard used
in PCs. Not all float/double numbers can be stored precisely. For example,
1/3 can't be stored precisely. Mathematically, 1/3 + 1/3 + 1/3 = 1. But in
computer, it may not be true.
212999 * 1.5 = 319498.5
21299.9 * 10 * 1.5 = 319498.5
2129.99 * 100 * 1.5 = 319498.49999999994
Math.round works as follows:
- for >= 0.5, it rounds up.
- for < 0.5, it rounds down.
So if you add 0.5, the Math.round gives you the right result:... 阅读全帖
k**n
发帖数: 3989
2
来自主题: Programming版 - 【急】JS round 运算出错如何破
比如
Math.round(212999*1.5)=319499
Math.round(21299.9*10*1.5)=319499
但是
Math.round(2129.99*100*1.5) =319498
头大了。。
(共0页)