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

fuyingch 2003-07-15 02:56:43
使用javascript做浮点运算时不能做到绝对精确,请问有什么办法可以解决。
parseFloat("5.000009")-parseFloat("4.000008") = 1.0000010000000005.;gai
...全文
37 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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
  • 打赏
  • 举报
回复
请问大侠门改如何解决这样的问题??急

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧