由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 【急】JS round 运算出错如何破
相关主题
浮点数运算等于0的问题round问题
一个dot net浮点运算的问题一道bit operator面试题
C++debug遇到的问题怎么编写改变数据类型的子程?
如何避免round off errorwhat is difference between 0x0008 and 8?
how to solve too large positive summation go to negative in fortran programming?double转换int的问题
C++ questionquestion about shift
print double auto precision?弱问c++里有没有NULL这个keyword?
tcl question请教:这个script运行后的not found 是怎么回事?
相关话题的讨论汇总
话题: 319499话题: 212999话题: round话题: decimals话题: 100
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
请教:这个script运行后的not found 是怎么回事?how to solve too large positive summation go to negative in fortran programming?
c++如何把小数转成二进制输出到文本文件?C++ question
base 13?print double auto precision?
C的argc问题tcl question
浮点数运算等于0的问题round问题
一个dot net浮点运算的问题一道bit operator面试题
C++debug遇到的问题怎么编写改变数据类型的子程?
如何避免round off errorwhat is difference between 0x0008 and 8?
相关话题的讨论汇总
话题: 319499话题: 212999话题: round话题: decimals话题: 100