2

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

4 回答 4

2

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

正确的。toFixed()是一种关于数字的方法。toFixed()返回一个字符串。所以rent + food不是执行加法,而是执行字符串连接。

仅调用toFixed()显示的值。不要将其返回值用于任何计算。

于 2017-02-17T04:44:25.857 回答
1

在将参数传递给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);
于 2017-02-17T04:51:15.430 回答
0

toFixed()以字符串形式返回数字,如果要比较数字,则需要parseFloat再次使用。

替代方案是

yourString= parseFloat((yourString).toFixed(2));
于 2017-02-17T04:47:24.260 回答
0

该方法toFixed(decimals)将您的号码转换为字符串。因此,在您的totalvar 中,您正在连接字符串而不是添加。你得到 "total.tofixed" is not a function是因为字符串没有toFixed()方法。

于 2017-02-17T04:50:43.013 回答