我的 toFixed() 方法有问题。在我将它添加到所有已经存在的 parseFloats 之前,它显示了所有总数,但小数位太多。现在它什么也没有显示。当我关闭 toFixed() 时,它会显示为应有的样子。控制台告诉我“total.tofixed”不是一个函数,但在我添加其他 6 个 toFixed() 命令之前,这部分是有效的。这是我的代码
var rent = prompt ("Enter your total rent");
var food = prompt ("Enter your total food expenditures");
var utilities = prompt ("Enter your total utilities costs");
var transport = prompt ("Enter your total transportations costs");
var internet = prompt ("Enter your internet and cable costs");
var other = prompt ("Enter an estimated total for all other expenditures");
rent = parseFloat(rent).toFixed(2);
food = parseFloat(food).toFixed(2);
utilities = parseFloat(utilities).toFixed(2);
transport = parseFloat(transport).toFixed(2);
internet = parseFloat(internet).toFixed(2);
other = parseFloat(other).toFixed(2);
var total = rent + food + utilities + transport + other;
total = total.toFixed(2); //determines "total" variable will use 2 decimal places
document.write(total);
var rentPerc = (rent / total)*100;
var foodPerc = (food / total)*100;
var utPerc = (utilities / total)*100;
var transPerc = (transport / total)*100;
var internetPerc = (internet / total)*100;
var otherPerc = (other / total)*100;
var totalPerc = rentPerc + foodPerc + utPerc + transPerc + internetPerc +otherPerc;
document.write("Total rent:", rent, rentPerc, "Total food", food, foodPerc, "Total utilities:",
utilities, utPerc, "Total transportation:", transport, transPerc, "Total internet:", internet,
internetPerc, "Total other:", other, otherPerc, "Total expenditures:", total, totalPerc);