限制 var 乘法总数中的位数
我有这个:
var ivu = (total3); document.getElementById("project_pay_total_field4").value = "$" + total3 * 0.07 ;
乘法结果是:
$10.080000000000002
--它太长了,在普通计算器中计算时它只显示 10.08;我该如何解决这个问题?
这是函数:
<script type = "text/javascript">
function project_pay_detail() {
var rate = document.getElementById("project_rate_field").value;
var time = document.getElementById("project_time_field").value;
var total1 = (rate * time);
document.getElementById("project_pay_total_field").value = "$" + total1 + ".00";
var rate = document.getElementById("project_rate_field2").value;
var time = document.getElementById("project_time_field2").value;
var total2 = (rate * time);
document.getElementById("project_pay_total_field2").value = "$" + total2 + ".00";
var total3 = (total1 + total2);
document.getElementById("project_pay_total_field3").value = "$" + total3 + ".00" ;
var ivu = (total3);
document.getElementById("project_pay_total_field4").value = "$" + total3 * 0.07 ;
}
</script>
这是表单
<div id="project_pay">Pay Rate<input type="text" name="project_rate_field" id="project_rate_field" class="field" value="" /></div>
<div id="project_pay">Total Hours<input type="text" name="project_time_field" id="project_time_field" class="field" value="" /></div>
<div id="project_pay">Project Total<input type="text" name="project_pay_total_field" id="project_pay_total_field" class="field" readonly="readonly" value="" /></div>
<input name="project_pay_details_calculate" type="button" value="Calculate1" onClick="project_pay_detail()" /></input>
<br>
<div id="project_pay">Pay Rate2<input type="text" name="project_rate_field2" id="project_rate_field2" class="field" value="" /></div>
<div id="project_pay">Total Hours2<input type="text" name="project_time_field2" id="project_time_field2" class="field" value="" /></div>
<div id="project_pay">Project Total2<input type="text" name="project_pay_total_field2" id="project_pay_total_field2" class="field" title="0.00" readonly="readonly" value="" /></div>
<input name="project_pay_details_refresh" type="button" value="Calculate2" onClick="project_pay_detail()" /></input>
<div id="project_pay">Total<input type="text" name="project_pay_total_field3" id="project_pay_total_field3" class="field" title="0.00" readonly="readonly" value="" /></div>
<div id="project_pay">Ivu<input type="text" name="project_pay_total_field4" id="project_pay_total_field4" class="field" title="0.00" readonly="readonly" value="" /></div>
</div>
I have this:
var ivu = (total3);
document.getElementById("project_pay_total_field4").value = "$" + total3 * 0.07 ;
The multiplication produces this:
$10.080000000000002
--Its too long and when done in regular calculator it only says 10.08; how can i fix that?
Here is the function:
<script type = "text/javascript">
function project_pay_detail() {
var rate = document.getElementById("project_rate_field").value;
var time = document.getElementById("project_time_field").value;
var total1 = (rate * time);
document.getElementById("project_pay_total_field").value = "$" + total1 + ".00";
var rate = document.getElementById("project_rate_field2").value;
var time = document.getElementById("project_time_field2").value;
var total2 = (rate * time);
document.getElementById("project_pay_total_field2").value = "$" + total2 + ".00";
var total3 = (total1 + total2);
document.getElementById("project_pay_total_field3").value = "$" + total3 + ".00" ;
var ivu = (total3);
document.getElementById("project_pay_total_field4").value = "$" + total3 * 0.07 ;
}
</script>
Heres is the form
<div id="project_pay">Pay Rate<input type="text" name="project_rate_field" id="project_rate_field" class="field" value="" /></div> <div id="project_pay">Total Hours<input type="text" name="project_time_field" id="project_time_field" class="field" value="" /></div> <div id="project_pay">Project Total<input type="text" name="project_pay_total_field" id="project_pay_total_field" class="field" readonly="readonly" value="" /></div> <input name="project_pay_details_calculate" type="button" value="Calculate1" onClick="project_pay_detail()" /></input> <br> <div id="project_pay">Pay Rate2<input type="text" name="project_rate_field2" id="project_rate_field2" class="field" value="" /></div> <div id="project_pay">Total Hours2<input type="text" name="project_time_field2" id="project_time_field2" class="field" value="" /></div> <div id="project_pay">Project Total2<input type="text" name="project_pay_total_field2" id="project_pay_total_field2" class="field" title="0.00" readonly="readonly" value="" /></div> <input name="project_pay_details_refresh" type="button" value="Calculate2" onClick="project_pay_detail()" /></input> <div id="project_pay">Total<input type="text" name="project_pay_total_field3" id="project_pay_total_field3" class="field" title="0.00" readonly="readonly" value="" /></div> <div id="project_pay">Ivu<input type="text" name="project_pay_total_field4" id="project_pay_total_field4" class="field" title="0.00" readonly="readonly" value="" /></div> </div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用:number.toFixed(x)
http://www.w3schools .com/jsref/jsref_tofixed.asp
try using: number.toFixed(x)
http://www.w3schools.com/jsref/jsref_tofixed.asp