我有一些代码涉及我试图只显示两位小数的计算。我正在使用 .toFixed(2),但它仍然无法正常工作。我把它放在正确的位置了吗?
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';
}
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;
form.subtotal.value = '$' + SetFoodItems(subtotal).toFixed(2);
var tax = (UpdateCosts / 100 * (form.rate.value - 0).toFixed(2);
tax = Math.floor(tax * 1000) / 1000;
form.tax.value = '$' + SetFoodItems(tax);
total = UpdateCosts + tax;
total = Math.floor((total * 1000) / 1000);
form.total.value = '$' + SetFoodItems(total).toFixed(2;