我的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);发布于 2017-02-17 12:44:26
,但在我添加其他6个toFixed()命令之前,这部分就可以工作了
正确的。toFixed()是numbers上的一种方法。toFixed()返回一个字符串。所以rent + food不是在执行加法,而是在执行字符串连接。
仅对要显示的值调用toFixed()。不要在任何计算中使用它的返回值。
发布于 2017-02-17 12:51:15
在将参数传递给toFixed之前,我仅使用parseFloat进行了一次更改
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);发布于 2017-02-17 12:47:24
toFixed()以字符串形式返回数字,如果要比较数字,则需要再次使用parseFloat。
另一种选择是
yourString= parseFloat((yourString).toFixed(2));https://stackoverflow.com/questions/42289537
复制相似问题