2017-02-17 38 views
0

我遇到了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); 
+0

你需要轉達rt那些字符串回到數字:'... = +租+ +食物+ +公共事業+ +運輸+ +其他;'。 – RobG

回答

2

但這一部分工作之前,我在其他6 toFixed()添加命令

權。 toFixed()號碼上的方法。 toFixed()返回一個字符串。所以rent + food不執行加法,它執行字符串連接。

只需撥打toFixed()就可以在顯示的值。不要將其返回值用於任何計算。

0

toFixed()返回數字爲字符串,如果您要比較數字,則需要再次使用parseFloat

另一種方法是

yourString= parseFloat((yourString).toFixed(2)); 
+0

謝謝,我試過這個,但是結果爲我工作的是先做parseFloats,然後是計算,最後是toFixed,分開。 – maria

0

的方法toFixed(decimals)你的電話號碼轉換爲字符串。所以在你的total var你連接字符串,而不是添加。你得到 "total.tofixed" is not a function,因爲一個字符串沒有toFixed()方法。

0

我僅取得了1個改變使用parseFloat參數傳遞給前toFixed

total = total.toFixed(2); //determines "total" variable will use 2 decimal places 

total = parseFloat(total).toFixed(2); //determines "total" variable will use 2 decimal places 

這裏是你的整個更新的代碼再次

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 = parseFloat(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); 
+0

仍然沒有工作。 'var total = rent + food + ...'會連接字符串。 – RobG

+1

它現在正在工作,謝謝。 – maria

+0

@Maria請接受正確的答案,如果你覺得它有用。它激勵人們回答你的問題。 – codemirror