请教javascript计算的精度问题,高分请教!

fuyingch 2003-07-15 02:56:43
使用javascript做浮点运算时不能做到绝对精确,请问有什么办法可以解决。
parseFloat("5.000009")-parseFloat("4.000008") = 1.0000010000000005.;gai
...全文
9 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ops2000 2003-07-15
注意必须是js5.5以上(ie5.5以上即可)
toFixed() Method
Returns a string representing a number in fixed-point notation.

numObj.toFixed([fractionDigits])

Arguments
numObj

Required A Number object.

fractionDigits

Optional. Number of digits after the decimal point. Must be in the range 0 – 20, inclusive.

Remarks
The toFixed method returns a string representation of a number in fixed-point notation. The string contains one digit before the significand's decimal point, and must contain fractionDigits digits after it.

If fractionDigits is not supplied or undefined, the toFixed method assumes the value is zero.

Requirements
Version 5.5

回复
wsj 2003-07-15
秋水的代码:

<script>
function qswhDiv(p1,p2){
var t1=0,t2=0,r1,r2;
try{t1=p1.toString().split(".")[1].length}catch(e){}
try{t2=p2.toString().split(".")[1].length}catch(e){}
with(Math){
r1=p1*pow(10,t1)
r2=p2*pow(10,t2)
return (r1/r2)*pow(10,t2-t1);
}
}
alert(qswhDiv(19879,19.879))
alert(qswhDiv(19999,199.99))
</script>

或者用vbscript

alert (19879/19.879);
execScript('alert(19879/19.879)','vbscript')
回复
fuyingch 2003-07-15
请问大侠门改如何解决这样的问题??急
回复
相关推荐
发帖
JavaScript
创建于2007-09-28

8.5w+

社区成员

Web 开发 JavaScript
申请成为版主
帖子事件
创建了帖子
2003-07-15 02:56
社区公告
暂无公告