5

I have the variable y, which is a subtotal. Its value is different depending on what happens with the html, but throughout the script I declared it like this:

var y = 21.78;

etc. Why is it that on my last equation where I add up the total, it treats them as strings when I want to add the values?

var tax = (0.055*y).toFixed(2);
var totalprice = y+tax;
/* totalprice holds "21.781.20" instead of 22.98 */
5
  • @Radu no, just a series of if else statements. y is always equal to a number like that, no other variables are involved in declaring it. Commented Apr 9, 2012 at 21:23
  • 3
    For a question to be answerable, the problem must be reproducible. Please include minimal sample code: complete, concise and representative. Read Writing the Perfect Queston for more guidelines. Commented Apr 9, 2012 at 21:23
  • @outis - ooh I like that answer. I'm gonna have to save that somewhere so I can re-use it. Commented Apr 9, 2012 at 21:23
  • Actually, it looks like the sample code is complete. It' so rare that three lines is enough, I leapt to the conclusion it wasn't. Commented Apr 9, 2012 at 21:26
  • He could have done it with one line using commas. Commented Apr 9, 2012 at 22:18

5 Answers 5

10

According to:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number/toFixed

toFixed() returns:

A string representation of number that does not use exponential notation and has exactly digits digits after the decimal place.

thus, y+tax is cast to a string since one of the operands is a string.

In my opinion, this would make sense as Javascript's intrinsic numeric types do not have the ability to store a specific number of decimal place digits, so a string would be the most appropriate data structure to store this with.

I would advise you do all your addition before calling toFixed(), since the method is most suitable for formatting display output.

var taxRate = 0.055;
var subtotal = 21.78;

var tax = (taxRate * subtotal).toFixed(2),
totalprice = ((1+taxRate) * subtotal).toFixed(2);
document.write(totalprice);

Sign up to request clarification or add additional context in comments.

Comments

3

The .toFixed() method returns a string. Try applying that method as the last step after all other calculations.

Comments

3

Here's a simple fix. Put '+' in front of the tax variable to convert it to a number.

var y = 21.78;
var tax = (0.055*y).toFixed(2);
var totalprice = y+ (+tax);
totalprice === 22.98;

If you don't want any rounding errors when you use toFixed, then include this re-implementation of it in your script. http://bateru.com/news/2012/03/reimplementation-of-number-prototype-tofixed/

Comments

1

In my experience, if there's any chance available, Javascript will see the "+" sign as concatenate rather than addition. It's driven me nuts on more than one occasion. I will generally do this rather than chance concatenation:

var totalprice = parseInt(y)+parseInt(tax);

1 Comment

typeof is your friend. So is an hour or two of googling how JS's dynamic type casting/conversion works. Much easier than hitting everything with the parse mallet which would have caused you problems in this case. You'll want parseFloat for numbers with decimals. parseInt(0.55) returns 0.
-1

When letter replaces value, multiply with 1 when you're in need of +.

var totalprice = (y*1) + tax . 

Other operands work fine, it's just the + operand that needs special treatment when variable replace value.

1 Comment

Did you test this? It doesn't work. y is already a number and multiplying it by 1 won't do anything. The problem is that tax is a string. Also, what's the period doing at the end of the line?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.