百思不得其解的问题,大家请看...

skimeister 2003-01-22 10:29:07
大家看看源代码,
当输入单价有2位小数时计算出错!
请高手更正下...

<html>
<head>
<title></title>
</head>
<body>
<table width="100%" border="1">
<tr>
<td>单价:</td>
<td>
<input name="price" type="text" value="5.56" size="10" onFocus="select();">
</td>
</tr>
<tr>
<td>数量:</td>
<td>
<input name="amount" type="text" value="10" size="10" onFocus="select();">
</td>
</tr>
<tr>
<td>  </td>
<td>合计:
<input name="total" type="text" size="10" onFocus="this.value=price.value*amount.value">
<font color="#FF0000"><----------大家点这里看看</font></td>
</tr>
</table>
</body>
</html>
...全文
50 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiushuiwuhen 2003-01-24
  • 打赏
  • 举报
回复
<script>
alert(qswhMul(56.10,23.12));
function qswhMul(n1,n2){
var m=0,s1=n1.toString(),s2=n2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}
</script>
烂笔头Danky 2003-01-23
  • 打赏
  • 举报
回复
浮点数的计算能力差一直是js的一大弱点!
jsidiot 2003-01-22
  • 打赏
  • 举报
回复
呵呵
不错。记着
saucer 2003-01-22
  • 打赏
  • 举报
回复
rounding errors, try
this.value= (price.value*amount.value).toFixed(3)

87,994

社区成员

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

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