JavaScriptstring和数字转换

我如何在JavaScript中执行以下操作?

  1. 将“1”,“2”,“3”连接成“123”

  2. 将“123”转换为123

  3. 加123 + 100 = 223

  4. 隐蔽223成“223”

你想熟悉parseInt()toString()

在你的工具包中有用的是查看一个variables来找出它是什么types – typeof

 <script type="text/javascript"> /** * print out the value and the type of the variable passed in */ function printWithType(val) { document.write('<pre>'); document.write(val); document.write(' '); document.writeln(typeof val); document.write('</pre>'); } var a = "1", b = "2", c = "3", result; // Step (1) Concatenate "1", "2", "3" into "123" // - concatenation operator is just "+", as long // as all the items are strings, this works result = a + b + c; printWithType(result); //123 string // - If they were not strings you could do result = a.toString() + b.toString() + c.toString(); printWithType(result); // 123 string // Step (2) Convert "123" into 123 result = parseInt(result,10); printWithType(result); // 123 number // Step (3) Add 123 + 100 = 223 result = result + 100; printWithType(result); // 223 number // Step (4) Convert 223 into "223" result = result.toString(); // printWithType(result); // 223 string // If you concatenate a number with a // blank string, you get a string result = result + ""; printWithType(result); //223 string </script> 

步骤(1)将“1”,“2”,“3”连接成“123”

  "1" + "2" + "3" 

步骤(2)将“123”转换为123

  parseInt("123", 10) 

步骤(3)添加123 + 100 = 223

  123 + 100 

步骤(4)将223隐藏成“223”

  (223).toString() 

把它们放在一起:

  (parseInt("1" + "2" + "3", 10) + 100).toString() 
 r = ("1"+"2"+"3") // step1 | build string ==> "123" r = +r // step2 | to number ==> 123 r = r+100 // step3 | +100 ==> 223 r = ""+r // step4 | to string ==> "223" //in one line r = ""+(+("1"+"2"+"3")+100); 

由于JavaScript的input系统,这些问题总是出现。 人们认为当他们得到一串数字时,他们正在得到一个数字。

以下是一些您可能会看到的利用JavaScript处理string和数字的方式。 就个人而言,我希望JavaScript使用了除+以外的符号来表示string连接。

步骤(1)将“1”,“2”,“3”连接成“123”

 result = "1" + "2" + "3"; 

步骤(2)将“123”转换为123

 result = +"123"; 

步骤(3)添加123 + 100 = 223

 result = 123 + 100; 

步骤(4)将223转换为“223”

 result = "" + 223; 

如果你知道为什么这些工作,你不太可能遇到JavaScriptexpression式的问题。

你可以这样做:

 // step 1 var one = "1" + "2" + "3"; // string value "123" // step 2 var two = parseInt(one); // integer value 123 // step 3 var three = 123 + 100; // integer value 223 // step 4 var four = three.toString(); // string value "223" 

要将string转换为数字,请减去0.要将数字转换为string,请添加“”(空string)。

5 + 1会给你6

(5 +“”)+ 1会给你“51”

(“5” – 0)+ 1会给你6

parseInt像scanf一样是错误的:

 parseInt(“12个猴子”,10)是一个数值为“12”
 +“12只猴子”是一个数值为“NaN”
数字(“12只猴子”)是一个数值为“NaN”

下面是JavaScript如何让你陷入麻烦的一个非常令人烦恼的例子:

如果您只是尝试使用parseInt()转换为数字,然后将另一个数字添加到结果它将连接两个string。

但是,您可以通过将总和expression式放在括号中来解决问题,如下例所示。

结果:他们的年龄是:98; 他们的年龄不是:5048

 <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } var myFather = new Person("John", "Doe", "50", "blue"); var myMother = new Person("Sally", "Rally", 48, "green"); document.getElementById("demo").innerHTML = "Their age sum is: "+ (parseInt(myFather.age)+myMother.age)+"; Their age sum is NOT: " + parseInt(myFather.age)+myMother.age; </script> </body> </html> 

最简单的是当你想要一个string做一个整数

 var a,b, c; a = 1; b = a.toString(); // This will give you string 

现在,从stringtypes的variablesb中可以得到整数

 c = b *1; //This will give you integer value of number :-) 

如果你想检查上面是一个数字。 如果你不确定b是否包含整数,那么你可以使用

 if(isNaN(c*1)) { //NOt a number } else //number