[英]Javascript is treating variables as Strings, why?
I have the variable y, which is a subtotal.我有变量 y,它是一个小计。 Its value is different depending on what happens with the html, but throughout the script I declared it like this:
它的值根据 html 发生的情况而有所不同,但在整个脚本中我是这样声明的:
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 */
According to:根据:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number/toFixed https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number/toFixed
toFixed()
returns: toFixed()
返回:
A string representation of number that does not use exponential notation and has exactly digits digits after the decimal place.
数字的字符串表示形式,不使用指数表示法且小数点后有 digits 数字。
thus, y+tax
is cast to a string since one of the operands is a string.因此,
y+tax
被转换为一个字符串,因为其中一个操作数是一个字符串。
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.在我看来,这是有道理的,因为 Javascript 的固有数字类型无法存储特定数量的小数位数字,因此字符串将是最合适的数据结构来存储它。
I would advise you do all your addition before calling toFixed()
, since the method is most suitable for formatting display output.我建议您在调用
toFixed()
之前完成所有添加,因为该方法最适合格式化显示 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. .toFixed() 方法返回一个字符串。 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.如果您在使用 toFixed 时不希望出现任何舍入错误,请在您的脚本中包含它的重新实现。 http://bateru.com/news/2012/03/reimplementation-of-number-prototype-tofixed/
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.根据我的经验,如果有任何机会,Javascript 会将“+”符号视为连接而不是加法。 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 +
.当字母替换值时,当您需要
+
时乘以1
。
var totalprice = (y*1) + tax .
Other operands work fine, it's just the +
operand that needs special treatment when variable replace value.其他操作数工作正常,只是
+
操作数在变量替换值时需要特殊处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.