var乘法总数中的限制数字

时间:2011-01-22 04:21:56

标签: javascript numbers rounding

我有这个:

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>

Heres是表格

<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>

2 个答案:

答案 0 :(得分:2)

尝试使用:数字 .toFixed(x)

var result = (total3 * 0.07);

document.getElementById("project_pay_total_field4").value = "$" + result.toFixed(2);

http://www.w3schools.com/jsref/jsref_tofixed.asp

答案 1 :(得分:1)


total3 = Math.round(total3 * 100) / 100;