JavaScript字符串和数字转换

时间:2009-06-09 16:08:08

标签: javascript string numbers

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

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

  2. 将“123”转换为123

  3. 添加123 + 100 = 223

  4. 将223隐藏为“223”

10 个答案:

答案 0 :(得分:117)

您希望熟悉parseInt()toString()

在您的工具包中有用的是查看变量以找出它的类型 - 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 :(得分:50)

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

 "1" + "2" + "3"

 ["1", "2", "3"].join("")

join方法将数组的项连接成一个字符串,在项之间放置指定的分隔符。在这种情况下,“分隔符”是一个空字符串("")。


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

 parseInt("123")

ECMAScript 5之前,有必要传递基数为10的基数:parseInt("123", 10)


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

 123 + 100


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

 (223).toString() 


Put It All Togther

 (parseInt("1" + "2" + "3") + 100).toString()

 (parseInt(["1", "2", "3"].join("")) + 100).toString()

答案 2 :(得分:26)

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);

答案 3 :(得分:14)

由于JavaScript的打字系统,这些问题一直存在。人们认为他们在得到一个数字字符串时会得到一个数字。

以下是您可能会看到的一些利用JavaScript处理字符串和数字的方式。就个人而言,我希望JavaScript使用除 + 之外的其他符号进行字符串连接。

步骤(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;

如果你知道为什么这些工作,你就不太可能遇到JavaScript表达式的麻烦。

答案 4 :(得分:11)

你可以这样做:

// 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"

答案 5 :(得分:8)

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

5 + 1会给你6

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

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

答案 6 :(得分:5)

parseInt像scanf一样错误:

parseInt("12 monkeys", 10) is a number with value '12'
+"12 monkeys"              is a number with value 'NaN'
Number("12 monkeys")       is a number with value 'NaN'

答案 7 :(得分:1)

以下是一个非常恼人的例子,说明JavaScript如何让您陷入麻烦:

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

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

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

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;

答案 8 :(得分:0)

最简单的是 当你想要一个整数字符串做

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

现在,从变量b的类型为string,我们可以得到整数

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

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

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

答案 9 :(得分:0)

我们可以通过使用一元加运算符将它们首先转换为数字并简单地相加来实现。见下文:-

var a = "4";
var b = "7";
var sum = +a + +b;