最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript is treating variables as Strings, why? - Stack Overflow

programmeradmin0浏览0评论

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 */

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 */
Share Improve this question edited Apr 9, 2012 at 21:28 outis 77.4k23 gold badges153 silver badges226 bronze badges asked Apr 9, 2012 at 21:20 WilsonWilson 8,76820 gold badges72 silver badges102 bronze badges 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. – Wilson 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. – outis 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. – j08691 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. – outis Commented Apr 9, 2012 at 21:26
  • He could have done it with one line using commas. – Erik Reppen Commented Apr 9, 2012 at 22:18
Add a comment  | 

5 Answers 5

Reset to default 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);

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

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/

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

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论