添加两个数量 [英] adding two quantities
问题描述
也许这是一个新手问题,但如果我有两个字符串,让我们说's1 =" 4"
和s2 =" 5",我怎么能得到一个新的值字符串9,即添加两个
数字。如果我输入:
var newStr = s1 + s2
我得到的是串联。
如何真正添加它们?
Thx
Maybe it''s a newbie question, but if I have two strings, let''s say s1="4"
and s2="5", how can I get a new string of value "9", that is, add the two
numbers. If I type:
var newStr = s1 + s2
all I get is a concatenation.
How to really add them?
Thx
推荐答案
yukatan,于2003年10月30日星期四20:59:09 +0100,不得不说:
yukatan, on Thu, 30 Oct 2003 20:59:09 +0100, had to say:
也许这是一个新手问题,但如果我有两个字符串,让我们说's1 =" 4"
和s2 =" 5",我该怎么办?得到一个值为9的新字符串,即添加两个
数字。如果我输入:
var newStr = s1 + s2
我得到的是串联。
如何真正添加它们?
Thx
Maybe it''s a newbie question, but if I have two strings, let''s say s1="4"
and s2="5", how can I get a new string of value "9", that is, add the two
numbers. If I type:
var newStr = s1 + s2
all I get is a concatenation.
How to really add them?
Thx
尝试:
var newStr = eval(s1)+ eval(s2);
干杯,
Mikhail
Try:
var newStr = eval(s1) + eval(s2);
Cheers,
Mikhail
Mikhail Esteves在comp.lang.javascript上写了30 okt 2003 :
Mikhail Esteves wrote on 30 okt 2003 in comp.lang.javascript:
yukatan,2003年10月30日星期四20:59:09 +0100,不得不说:
yukatan, on Thu, 30 Oct 2003 20:59:09 +0100, had to say:
也许这是一个新手问题,但如果我有两个字符串,让我们说
s1 =" 4"并且s2 =5,如何获得值为9的新字符串,即
添加两个数字。如果我输入:
var newStr = s1 + s2
我得到的是串联。
如何真正添加它们?
Thx
Maybe it''s a newbie question, but if I have two strings, let''s say
s1="4" and s2="5", how can I get a new string of value "9", that is,
add the two numbers. If I type:
var newStr = s1 + s2
all I get is a concatenation.
How to really add them?
Thx
var newStr = eval(s1)+ eval(s2);
Try:
var newStr = eval(s1) + eval(s2);
永远不要使用eval,它是邪恶的。
使用:
var newStr = 1 * s1 + 1 * s2
或
var newStr = + s1 + + s2
-
Evertjan。
荷兰。
(请在我的电子邮件地址中将x'变为点数)
Never use eval, it is evil.
Use:
var newStr = 1*s1 + 1*s2
or
var newStr = +s1 + +s2
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)
> >>也许这是一个新手问题,但如果我有两个字符串,那么让我们说
> >> Maybe it''s a newbie question, but if I have two strings, let''s say
s1 =" 4"并且s2 =5,如何获得值为9的新字符串,即
添加两个数字。如果我输入:
var newStr = s1 + s2
我得到的只是串联。 var newStr = eval(s1)+ eval(s2);
s1="4" and s2="5", how can I get a new string of value "9", that is,
add the two numbers. If I type:
var newStr = s1 + s2
all I get is a concatenation. var newStr = eval(s1) + eval(s2);
永远不要使用eval,这是邪恶的。
使用:
var newStr = 1 * s1 + 1 * s2
var newStr = + s1 + + s2
Never use eval, it is evil.
Use:
var newStr = 1*s1 + 1*s2
or
var newStr = +s1 + +s2
最后一种形式绝对是最好的。我建议给它添加一些parens
var newStr = + s1 +(+ s2);
这样就不会与它混淆br />
var newStr = + s1 ++ s2;
http://www.crockford.com
The last form is definitely the best. I recommend adding parens to it
var newStr = +s1 + (+s2);
so that it will not be confused with
var newStr = +s1 ++s2;
http://www.crockford.com
这篇关于添加两个数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!