将字符串更改为数字(简单...也许) [英] Changing strings to numbers(easy...maybe)

查看:89
本文介绍了将字符串更改为数字(简单...也许)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图将3个数字加在一起,而不是得到1 + 2 + 3 = 6,我是

得到123我怎么能解决这个问题。


提前许多谢谢


Michael


< html> ;

< head>

< title>求和模板文件< / title>

< meta http-equiv ="内容型" content =" text / html;

charset = iso-8859-1">

< script language =" JavaScript">

function addUp(){

var a,b,c,sum;

a = document.fred.aaa.value; //提取输入框中输入的文本字符串



b = document.fred.bbb.value;

c = document.fred。 ccc.value;

//问题出在下一行,所有其他行都没问题,

不应该改变

sum = a + b + c;

window.alert(总和是+总和);

}

< ; / script>

< / head>


< body bgcolor =" #FFFFFF" text ="#000000">

< form name =" fred">

< input type =" text"名称= QUOT; AAA" size =" 5">

< input type =" text"名称= QUOT; BBB" size =" 5">

< input type =" text"名称= QUOT; CCC" size =" 5">

< input type =" button" value =" Sum Data" onClick =" addUp();">

< / form>

< / body>

< / html> ;

Hi,

Im trying to add 3 numbers together, instead of getting 1+2+3=6, I''m
getting 123, how could I remedy this.

ManyThanks in advance

Michael

<html>
<head>
<title>Summation Template File</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript">
function addUp() {
var a,b,c,sum;
a = document.fred.aaa.value; // extract the string of text typed into
the input box
b = document.fred.bbb.value;
c = document.fred.ccc.value;
// the problem is on the next line, all the other lines are OK and
shouldn''t be changed
sum =a+b+c ;
window.alert("The sum is "+sum);
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="fred">
<input type="text" name="aaa" size="5">
<input type="text" name="bbb" size="5">
<input type="text" name="ccc" size="5">
<input type="button" value="Sum Data" onClick="addUp();">
</form>
</body>
</html>

推荐答案

MickG写道:
MickG wrote:


我试图将3个数字加在一起,而不是得到1 + 2 + 3 = 6,我得到123,我怎么能解决这个问题。
Hi,

Im trying to add 3 numbers together, instead of getting 1+2+3=6, I''m
getting 123, how could I remedy this.




使用parseInt(number)得到一个数字


Kae



use parseInt(number) to get a number

Kae


MickG于2005年2月28日在comp.lang.javascript中写道
MickG wrote on 28 feb 2005 in comp.lang.javascript:
我试图将3个数字加在一起,而不是得到1 + 2 + 3 = 6,我是
得到123,怎么可能我解决了这个问题。
Im trying to add 3 numbers together, instead of getting 1+2+3=6, I''m
getting 123, how could I remedy this.




var a =''1''//字符串!

var b =''2''//字符串!

var c =''3''//字符串!


r = a + b + c


alert(r)//返回字符串123


r = + a + + b + + c


alert(r)//返回numbe r 6


r = 1 * a + 1 * b + 1 * c

alert(r)//返回6号
-

Evertjan。

荷兰。

(用我的电子邮件地址替换所有带点的十字架)



var a=''1'' // string !
var b=''2'' // string !
var c=''3'' // string !

r = a + b + c

alert(r) // returns string 123

r = +a + +b + +c

alert(r) // returns number 6

r = 1*a + 1*b + 1*c

alert(r) // returns number 6
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


>我正在尝试将3个数字加在一起,而不是得到1 + 2 + 3 = 6,我是
> I''m trying to add 3 numbers together, instead of getting 1+2+3=6, I''m
得到123,我怎么能解决这个问题。
a = document.fred.aaa.value; //提取键入的文本字符串
//输入框
b = document.fred.bbb.value;
c = document.fred.ccc.value;
//问题出在下一行,所有其他行都没问题,
//不应该改变
sum = a + b + c;
getting 123, how could I remedy this. a = document.fred.aaa.value; // extract the string of text typed into
// the input box
b = document.fred.bbb.value;
c = document.fred.ccc.value;
// the problem is on the next line, all the other lines are OK and
// shouldn''t be changed
sum =a+b+c ;




sum =(+ a)+(+ b)+(+ c);


http://www.crockford.com/javascript/survey.html


如果你遗漏了parens,它可能看起来像


sum = + a ++ b ++ c;


这将是非常的坏。最好清楚。



sum = (+a) + (+b) + (+c);

See http://www.crockford.com/javascript/survey.html

If you leave out the parens, it could look like

sum = +a++b++c;

which would be very bad. It is better to be clear.


这篇关于将字符串更改为数字(简单...也许)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆