我的一部分代码处理数学和求和。大多数方程等于小数点后三位,但我想将其固定为 2。我知道使用 tofixed(2),但我将函数放在哪里似乎并不重要,数字保持为小数点后三位。我确定我犯了一些愚蠢的错误
<script language="JavaScript">
function SetFoodItems(amount) {
// returns the amount in the .99 format
return (amount == Math.floor(amount)) ? amount + '.00' : ((amount * 10
== Math.floor(amount * 10)) ? amount + '0' : amount);
}
function SelectFoodItems(form) {
var UpdateCosts = (form.quantity.value - 0) * (form.unitcost.value -
0) + (form.quantity1.value - 0) * (form.unitcost1.value - 0)
(form.quantity2.value - 0) * (form.unitcost2.value - 0) +
(form.quantity3.value - 0) * (form.unitcost3.value - 0).toFixed(2);
UpdateCosts = Math.floor((subtotal * 1000) / 1000).toFixed(2);
form.subtotal.value = ('$' + cent(subtotal));
var tax = (UpdateCosts / 100 * (form.rate.value - 0).toFixed(2);
tax = Math.floor(tax * 1000) / 1000;
form.tax.value = '$' + cent(tax);
total = UpdateCosts + tax;
total = Math.floor((total * 1000) / 1000);
form.total.value = ('$' + cent(total)).toFixed(2);
}
</script>