92

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

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

  2. 将“123”转换为123

  3. 加 123 + 100 = 223

  4. 将 223 转换为“223”

4

10 回答 10

121

您想熟悉parseInt()toString()

在你的工具包中有用的是查看一个变量以找出它是什么类型—<a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof" rel ="noreferrer"> 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>
于 2009-06-09T16:22:39.260 回答
54

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


把它放在一起

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

或者

 (parseInt(["1", "2", "3"].join("")) + 100).toString()
于 2009-06-09T16:11:48.413 回答
27
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);
于 2011-09-28T09:49:45.597 回答
15

由于 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 表达式中遇到麻烦。

于 2009-06-09T16:25:46.000 回答
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"
于 2009-06-09T16:11:37.230 回答
9

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

5 + 1会给你6

(5 + "") + 1 会给你 "51"

("5" - 0) + 1 会给你 6

于 2009-09-04T05:56:02.110 回答
6

parseInt 像 scanf 一样错误:

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

于 2009-10-23T18:11:48.277 回答
1

下面是一个非常恼人的例子,说明 JavaScript 如何让你陷入困境:

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

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

结果:他们的年龄总和是: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>

于 2016-10-03T15:54:15.387 回答
0

最简单的是当你想把一个整数变成一个字符串时

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

现在,从字符串类型的变量 b 我们可以得到整数

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

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

if(isNaN(c*1)) {
  //NOt a number
}
else //number
于 2014-04-21T11:37:06.560 回答
0

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

var a = "4";
var b = "7";
var sum = +a + +b; 
于 2019-03-11T07:59:52.563 回答