簡體   English   中英

Javascript toFixed()問題

[英]Javascript toFixed() issues

我的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);

但是在我添加其他6個toFixed()命令之前,這部分已經正常工作

對。 toFixed()是對number的一種方法。 toFixed()返回一個字符串 因此, rent + food沒有執行加法,而是執行字符串連接。

僅在要顯示的值上調用toFixed() 不要將其返回值用於任何計算。

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

替代方法是

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

toFixed(decimals)方法將您的數字轉換為字符串。 因此,在您的total var中,您是連接字符串而不是添加字符串。 您得到的"total.tofixed" is not a function因為字符串沒有toFixed()方法。

在將參數傳遞給toFixed之前,我僅使用parseFloat進行了1次更改

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM