k**n 发帖数: 3989 | 1 比如
Math.round(212999*1.5)=319499
Math.round(21299.9*10*1.5)=319499
但是
Math.round(2129.99*100*1.5) =319498
头大了。。 |
j*a 发帖数: 14423 | 2 Math.round(212999*15/10)
【在 k**n 的大作中提到】 : 比如 : Math.round(212999*1.5)=319499 : Math.round(21299.9*10*1.5)=319499 : 但是 : Math.round(2129.99*100*1.5) =319498 : 头大了。。
|
k**n 发帖数: 3989 | 3 自己写了个简单的解决办法来round小数点后两位。。
return Math.round((Math.round(x*1000)/1000)*100)/100;
但遇到 9.9949 就不行了。。
还打算试试 Math.round(x*100+0.0000000001)/100 |
a9 发帖数: 21638 | 4 google is your friend
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
【在 k**n 的大作中提到】 : 自己写了个简单的解决办法来round小数点后两位。。 : return Math.round((Math.round(x*1000)/1000)*100)/100; : 但遇到 9.9949 就不行了。。 : 还打算试试 Math.round(x*100+0.0000000001)/100
|
o*********r 发帖数: 203 | 5 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:
212999 * 1.5 + 0.5 = 319499 -> round to 319499
21299.9 * 10 * 1.5 + 0.5 = 319499 -> round to 319499
2129.99 * 100 * 1.5 + 0.5 = 319498.99999999994 -> round to 319499 |
p****e 发帖数: 3548 | 6 http://www.google.com/url?sa=t&source=web&rct=j&url=https://dev
用上面的Math.round10
Math.round(Math.round10(expr, -5))
-5是你要的精度
used
,
【在 o*********r 的大作中提到】 : 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.
|