关于浮点数问题

netfound netfound 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
怎么样才能使计算出来的浮点数不会出现下面这种情况?
——————————————————————————————
第一组数: 1.1 1.2 1.3 1.4
第二组数: 2.1 2.2 2.3 2.4
合计: 3.2 3.40003 3.59996 3.8
——————————————————————————————
<script language="javascript" author="Http://www.TopScripts.net">
function count(num,step){
var obj,i,total=0;
for (i=((num % step==0)?step : (num % step));i<num;i+=step){
obj=eval("theForm.TextBox"+i);
total=total+parseFloat(obj.value);
}
return total;
}
function countNum(){
theForm.TextBox15.value=count(15,7)
theForm.TextBox16.value=count(16,7)
theForm.TextBox17.value=count(17,7)
theForm.TextBox18.value=count(18,7)
theForm.TextBox19.value=count(19,7)
theForm.TextBox20.value=count(20,7)
theForm.TextBox21.value=count(21,7)
}
setTimeout("countNum()",2000);
-->
</script>

<table align=center>
<form name="theForm">
<tr>
<td>第一组数:</td>
<td><input type="text" size="10" name="TextBox1" value="1.1"></td>
<td><input type="text" size="10" name="TextBox2" value="1.2"></td>
<td><input type="text" size="10" name="TextBox3" value="1.3"></td>
<td><input type="text" size="10" name="TextBox4" value="1.4"></td>
<td><input type="text" size="10" name="TextBox5" value="1.5"></td>
<td><input type="text" size="10" name="TextBox6" value="1.6"></td>
<td><input type="text" size="10" name="TextBox7" value="1.7"></td>
</tr>
<tr>
<td>第二组数:</td>
<td><input type="text" size="10" name="TextBox8" value="2.1"></td>
<td><input type="text" size="10" name="TextBox9" value="2.2" autocomplete="off"></td>
<td><input type="text" size="10" name="TextBox10" value="2.3" autocomplete="off"></td>
<td><input type="text" size="10" name="TextBox11" value="1.1"></td>
<td><input type="text" size="10" name="TextBox12" value="6.1"></td>
<td><input type="text" size="10" name="TextBox13" value="1.2"></td>
<td><input type="text" size="10" name="TextBox14" value="1.3"></td>
</tr>
<tr>
<td>合计:</td>
<td><input type="text" size="10" name="TextBox15"></td>
<td><input type="text" size="10" name="TextBox16"></td>
<td><input type="text" size="10" name="TextBox17"></td>
<td><input type="text" size="10" name="TextBox18""></td>
<td><input type="text" size="10" name="TextBox19"></td>
<td><input type="text" size="10" name="TextBox20"></td>
<td><input type="text" size="10" name="TextBox21"></td>
</tr>
</form>
</table>
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
netfound
JavaScript 87,984 成员 224,681 内容