Javascript 总和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7053511/
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
Javascript Sum Values
提问by Andha
I need to sum several values in javascript. I've tried by using following code
我需要在 javascript 中总结几个值。我已经尝试使用以下代码
var a = 2;
var b = 5;
c = a+b;
But, instead of calculating the values of a and b, the output (c) only combine those two values. So the output given is :
但是,输出 (c) 不是计算 a 和 b 的值,而是仅组合这两个值。所以给出的输出是:
c = 25
I believe you guys can help me easily about this. Thx before. Regard Andha.
我相信你们可以很容易地帮助我解决这个问题。之前谢谢。关注安达。
回答by kjetilh
Make sure the values are numbers, otherwise they will concat instead of suming.
确保这些值是数字,否则它们将串联而不是求和。
a = parseInt(a, 10); // a is now int
回答by numbers1311407
Your code is adding (concatenating) strings. Are you sure that the code you posted represents your problem? What you have written should work. Be sure in the real code you're not saying:
您的代码正在添加(连接)字符串。您确定您发布的代码代表您的问题吗?你写的应该有效。确保在真正的代码中你没有说:
var a = '2'; // or something similar
Or if the values are parsed from somewhere, be sure to call parseInt(a, 10)
on them before doing the addition, 10 being the radix.
或者,如果值是从某处解析的,请确保parseInt(a, 10)
在进行加法之前调用它们,10 是基数。
Or as pointed out in the comments the Number
function would probably suit your purposes.
或者正如评论中指出的那样,该Number
功能可能适合您的目的。
回答by dMole
The author has probably put "simplified" code so we can get an idea. Had same problem, while getting input values. JS interpreted it as string. Using "Number()" solved the problem:
作者可能已经放置了“简化”的代码,以便我们可以得到一个想法。在获取输入值时遇到了同样的问题。JS 将其解释为字符串。使用“Number()”解决了这个问题:
var sum = Number(document.getElementById("b4_f2_"+i).value) + Number(document.getElementById("b4_f3_"+i).value) + Number(document.getElementById("b4_f4_"+i).value);
回答by Jord?o
This works fine:
这工作正常:
var a = 2;
var b = 5;
var c = a + b; // c is now 7
回答by JAAulde
The code you show will not work the way you describe. It will result in 7
.
您显示的代码不会按照您描述的方式工作。它会导致7
.
However, when attempting to perform addition, if either or both numeric values are actually numeric strings, the other values will be cast to strings and they will be concatenated.
但是,在尝试执行加法时,如果其中一个或两个数值实际上是数字字符串,则其他值将被强制转换为字符串并将它们连接起来。
This is most likely to happen when attempting to read form values, reading cookies, or some other sort of HTTP header. To convert a string to a number, you need to use parseInt()
[docs]. Read through the docs on it and be sure to pay attention to, and provide, the second parameter (radix
)to ensure the casting from string to number uses the base you expect. (The lack of info on radix
in other answers is the primary reason I went ahead and posted an answer even though others had already mentioned parseInt()
.)
当尝试读取表单值、读取 cookie 或某种其他类型的 HTTP 标头时,最有可能发生这种情况。要将字符串转换为数字,您需要使用parseInt()
[ docs]。通读它的文档,并确保注意并提供第二个参数 ( radix
)以确保从字符串到数字的转换使用您期望的基数。(radix
其他答案中缺乏信息是我继续发布答案的主要原因,即使其他人已经提到了parseInt()
。)
Also, FYI, Another handy function to use when dealing with unknown values and hoping to perform mathematic operations is isNaN()
[docs].
另外,仅供参考,在处理未知值并希望执行数学运算时使用的另一个方便的函数是isNaN()
[ docs]。
回答by Murugan Boopathi
Use parseInt()
:
使用parseInt()
:
var a=2;
var b=5;
c=parseInt(a)+parseInt(b);
回答by Artemiy Egorov
You can simply convert string
to a number
by adding +
before it. For somebody can be more readable.
Example:
您可以通过在它之前添加来简单地转换string
为 a 。对于某些人来说,可读性更高。例子:number
+
const a = "2";
const b = "5";
const c = +a + +b
or const c = (+a) + (+b)
may be more readable.
That will first convert the string to a Number.
或者const c = (+a) + (+b)
可能更具可读性。这将首先将字符串转换为数字。