我遇到了toFixed()方法的問題。在我將它添加到已經存在的所有parseFloats之前,它顯示了所有的總數,但是有太多的小數位數。現在它什麼都沒顯示。當我將toFixed()關閉時,它顯示爲它應該。控制檯告訴我「total.tofixed」不是一個函數,但是在添加其他6個toFixed()命令之前,這部分工作正常。這裏是我的代碼JavaScript 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);
你需要轉達rt那些字符串回到數字:'... = +租+ +食物+ +公共事業+ +運輸+ +其他;'。 – RobG