2

I am having trouble with my toFixed() method. Before I added it onto all the parseFloats, which were already there, it was displaying all the totals but with too many decimal places. Now it displays nothing. When I take the toFixed() off, it displays like it should. Console is telling me "total.tofixed" is not a function, but this part was working before I added in the other 6 toFixed() commands. Here is my code

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);
maria
  • 41
  • 4
  • You need to convert those strings back to numbers: `... = +rent + +food + +utilities + +transport + +other;`. – RobG Feb 17 '17 at 04:44

4 Answers4

2

but this part was working before I added in the other 6 toFixed() commands

Right. toFixed() is a method on numbers. toFixed() returns a string. So rent + food is not performing addition, it's performing string concatenation.

Only call toFixed() on the values that you want to display. Don't use its return value for any computations.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

I have made only 1 change used parseFloat before passing the parameter to toFixed

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

to

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

Here is your whole updated code again

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);
codemirror
  • 3,164
  • 29
  • 42
0

toFixed() returns number as string, if you will compare numbers, you need to use parseFloat again.

The alternative is

yourString= parseFloat((yourString).toFixed(2));
shubham agrawal
  • 3,435
  • 6
  • 20
  • 31
  • thanks, i tried this but what ended up working for me was doing the parseFloats first, then the calculations, then finally the toFixed, separately. – maria Feb 17 '17 at 05:54
0

The method toFixed(decimals) converts your number to a string. So in your total var you're concatenating strings instead of adding. You're getting "total.tofixed" is not a functionbecause a string doesn't have a toFixed() method.

Gerardo
  • 979
  • 1
  • 7
  • 15