限制 var 乘法总数中的位数

发布于 2024-10-13 04:52:05 字数 2830 浏览 4 评论 0原文

我有这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-10-20 04:52:05

尝试使用:number.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

try using: number.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

假扮的天使 2024-10-20 04:52:05

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

total3 = Math.round(total3 * 100) / 100;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文