如何确保 javascript 添加而不是字符串连接(并不总是添加整数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12982128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to ensure javascript addition instead of string concatenation (Not always adding integers)
提问by dnc253
Through my javascript library, I end up with a string that represents a number. Now I want to preform an addition on that number without it doing a string concatenation instead. The solution is easy ( How do I add an integer value with javascript (jquery) to a value that's returning a string?, How to make an addition instead of a concatenation) if your number is always in integer. However my string could be a float or an integer, and at the time of the addition, and I don't know which it will be. Is there a way to make sure the addition happens regardless of whether it's a float or integer?
通过我的 javascript 库,我最终得到了一个代表数字的字符串。现在我想对该数字进行加法运算,而不是进行字符串连接。如果您的数字始终为整数,则解决方案很简单( 如何使用 javascript (jquery) 将整数值添加到返回字符串的值中?,如何进行加法而不是串联)。但是我的字符串可以是浮点数或整数,并且在添加时,我不知道它将是哪个。无论是浮点数还是整数,有没有办法确保加法发生?
回答by Ryan P
Try using parseFloat
. The input variables will be converted to floats, which will work whether the string contains an integer or a float. Your result will always be a float.
尝试使用parseFloat
. 输入变量将转换为浮点数,无论字符串包含整数还是浮点数,这都将起作用。你的结果将永远是一个浮动。
回答by Asad Saeeduddin
It is reasonably safe to always use parseFloat
even if you are given an integer. If you must use the appropriate conversion, you could match
for a decimal point and use the appropriate function.
parseFloat
即使给定一个整数,始终使用也是相当安全的。如果必须使用适当的转换,则可以match
换一个小数点并使用适当的函数。
回答by jcolebrand
I'm going to assume that float addition is sufficient, and if it isn't, then you'll need to dig deeper.
我将假设浮动添加就足够了,如果不是,那么您需要更深入地挖掘。
function alwaysAddAsNumbers(numberOne, numberTwo){
var parseOne = parseFloat(numberOne),
parseTwo = parseFloat(numberTwo);
if (isNaN(parseOne)) parseOne = 0;
if (isNaN(parseTwo)) parseTwo = 0;
return parseOne + parseTwo;
}
To take what @Asad said, you might want to do this instead:
按照@Asad 所说的,您可能想要这样做:
function alwaysAddAsNumbers(a, b){
var m = 0,
n = 0,
d = /\./,
f = parseFloat,
i = parseInt,
t = isNaN,
r = 10;
m = (d.test(a)) ? f(a) : i(a,r);
n = (d.test(b)) ? f(b) : i(b,r);
if (t(m)) m = 0;
if (t(n)) n = 0;
return m + n;
}
this will always give you at least a zero output, and doesn't tell you if either one is NaN
.
这将始终至少为您提供零输出,并且不会告诉您其中任何一个是否为NaN
.
回答by Bill Warren
For some reason.... who knows???
出于某种原因......谁知道???
Using parseFloat works.... when parseInt does not... obviously they are not always the same.
使用 parseFloat 工作......当 parseInt 不......显然它们并不总是相同的。
FWIW.... I'm "adding" data elements from "checked" checkboxes here...
FWIW ......我在这里“添加”来自“选中”复选框的数据元素......
var AddOns = 0;
$( '.broadcast-channels').each(function(){
if ( $(this).prop("checked")) {
var thisAddOn = parseFloat($(this).data('channel'));
AddOns = AddOns + thisAddOn;
}
});