原文: https://www.freecodecamp.org/news/how-to-convert-a-string-to-a-number-in-javascript/
有很多方法可以用 JavaScript 将一个字符串转换成一个数字。但这在代码中是什么样子的呢?
在这篇文章中,我将向你展示 11 种将字符串转换成数字的方法。
这里有一个关于如何在 JavaScript 中把字符串转换成数字的片段:
如何在 JavaScript 中使用 Number() 函数将一个字符串转换成一个数字
将字符串转换成数字的一种方法是使用 Number()
函数。
在这个例子中,我们有一个名为 quantity
的字符串,其值为 "12"
。
const quantity = "12";
如果我们对 quantity
使用 typeof
运算符,那么它将返回字符串的类型。
console.log(typeof quantity);
![Screen-Shot-2022-05-01-at-9.50.17-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-9.50.17-AM.png)
我们可以使用 Number
函数将 quantity
转换为数字,如图所示:
Number(quantity)
我们可以通过再次使用 typeof
操作符来检查它现在是一个数字。
console.log(typeof Number(quantity));
![Screen-Shot-2022-05-01-at-9.57.35-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-9.57.35-AM.png)
如果你试图传入一个无法转换为数字的值,那么返回值将是 NaN
(Not a Number 非数字)。
console.log(Number("awesome"));
![Screen-Shot-2022-05-01-at-10.00.34-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-10.00.34-AM.png)
如何在 JavaScript 中使用 parseInt() 函数将一个字符串转换成一个数字
另一种将字符串转换成数字的方法是使用 parseInt()
函数。这个函数接收一个字符串和一个可选的进制数。
进制数是一个介于 2 和 36 之间的数字,代表数字系统中的基数。例如,进制数 2 代表二进制系统,而进制数 10 代表十进制系统。
我们可以使用先前的 quantity
变量将该字符串转换为数字。
const quantity = "12";
console.log(parseInt(quantity, 10));
如果我试图将 quantity
变量改为 "12.99"
会怎样?使用 parseInt()
的结果会是 12.99 这个数字吗?
const quantity = "12.99";
console.log(parseInt(quantity, 10));
![Screen-Shot-2022-05-01-at-10.45.08-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-10.45.08-AM.png)
正如你所看到的,结果是一个四舍五入的整数。如果你想返回一个浮点数,那么你就需要使用 parseFloat()
。
如何在 JavaScript 中使用 parseFloat() 函数将一个字符串转换为一个数字
parseFloat()
函数将接收一个值并返回一个浮点数。浮点数的例子是 12.99 或 3.14。
如果我们修改前面的例子,使用 parseFloat()
,那么结果将是浮点数 12.99。
const quantity = "12.99";
console.log(parseFloat(quantity));
![Screen-Shot-2022-05-01-at-10.55.03-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-10.55.03-AM.png)
如果你的字符串中有前导或尾部的空白,那么 parseFloat()
仍然会将该字符串转换成浮点数。
const quantity = " 12.99 ";
console.log(parseFloat(quantity));
![Screen-Shot-2022-05-01-at-11.05.35-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-11.05.35-AM.png)
如果你的字符串中的第一个字符不能被转换为数字,那么 parseFloat()
将返回 NaN
。
const quantity = "F12.99";
console.log(parseFloat(quantity));
![Screen-Shot-2022-05-01-at-11.08.33-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-11.08.33-AM.png)
如何在 JavaScript 中使用加号运算符(+)将字符串转换为数字
加号运算符(+
)将把一个字符串转换成一个数字。该运算符将被放在操作数之前。
const quantity = "12";
console.log(+quantity);
![Screen-Shot-2022-05-01-at-11.14.51-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-11.14.51-AM.png)
我们还可以使用加号(+
)将字符串转换为浮点数。
const quantity = "12.99";
console.log(+quantity);
![Screen-Shot-2022-05-01-at-11.16.38-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-11.16.38-AM.png)
如果字符串值不能被转换为数字,那么结果将是 NaN
。
const quantity = "awesome";
console.log(+quantity);
![Screen-Shot-2022-05-01-at-11.18.10-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-11.18.10-AM.png)
如何在 JavaScript 中通过将字符串乘以数字 1 来将其转换为数字
另一种将字符串转换为数字的方法是使用基本的数学运算。你可以用字符串的值乘以 1,它将返回一个数字。
const quantity = "12";
console.log(quantity * 1);
![Screen-Shot-2022-05-01-at-11.42.58-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-11.42.58-AM.png)
正如你所看到的,当我们把 quantity
值乘以 1 时,就会返回数字 12。但这是如何进行的呢?
在这个例子中,JavaScript 正在将我们的字符串值转换为数字,然后执行该数学运算。 如果字符串不能被转换为数字,那么数学运算就不会起作用,它将返回 NaN
。
const quantity = "awesome";
console.log(quantity * 1);
![Screen-Shot-2022-05-01-at-11.18.10-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-11.18.10-AM.png)
这个方法也适用于浮点数。
const quantity = "10.5";
console.log(quantity * 1);
![Screen-Shot-2022-05-01-at-11.56.19-AM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-11.56.19-AM.png)
如何在 JavaScript 中通过用字符串除以数字 1 的方式将字符串转换为数字
你也可以不乘以 1,而是用字符串除以 1。JavaScript 是将我们的字符串值转换成数字,然后进行该数学运算。
const quantity = "10.5";
console.log(quantity / 1);
![Screen-Shot-2022-05-01-at-12.08.37-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.08.37-PM.png)
如何在 JavaScript 中通过从字符串中减去数字 0 将字符串转换为数字
另一种方法是从字符串中减去 0。像之前一样,JavaScript 是将我们的字符串值转换为数字,然后进行该数学运算。
const quantity = "19";
console.log(quantity - 0);
![Screen-Shot-2022-05-01-at-12.11.59-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.11.59-PM.png)
如何在 JavaScript 中使用按位非运算符(~)将字符串转换为数字
按位非运算符(~
)将反转一个操作数的位数,并将该值转换为 32 位有符号整数。一个 32 位有符号整数是用 32 位(或 4 个字节)表示一个整数的值。
如果我们在一个数字上使用一个按位非运算符(~
),那么它将执行这样的操作:-(x + 1)。
console.log(~19);
![Screen-Shot-2022-05-01-at-12.20.18-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.20.18-PM.png)
但如果我们使用两个按位非运算符(~
),那么它将把我们的字符串转换为数字。
const quantity = "19";
console.log(~~quantity);
![Screen-Shot-2022-05-01-at-12.28.16-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.28.16-PM.png)
这个方法对浮点数不起作用,因为结果会是一个整数。
const quantity = "19.99";
console.log(~~quantity);
![Screen-Shot-2022-05-01-at-12.31.16-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.31.16-PM.png)
如果你试图对非数字字符使用这种方法,那么结果将是 0。
const quantity = "awesome";
console.log(~~quantity);
![Screen-Shot-2022-05-01-at-12.32.45-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.32.45-PM.png)
这种方法确实有它的局限性,因为它对那些被认为太大的数值会开始崩溃。重要的是要确保你的数字是在 32 位有符号整数的数值之间。
const quantity = "2700000000";
console.log(~~quantity);
![Screen-Shot-2022-05-01-at-12.36.16-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.36.16-PM.png)
要了解更多关于按位非运算符(~
)的信息,请阅读文档。
如何在 JavaScript 中使用 Math.floor() 函数将一个字符串转换为一个数字
另一种将字符串转换为数字的方法是使用 Math.floor()
函数。这个函数将把数字四舍五入到最接近的整数。
const quantity = "13.4";
console.log(Math.floor(quantity));
![Screen-Shot-2022-05-01-at-12.44.53-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.44.53-PM.png)
就像前面的例子一样,如果我们试图使用非数字字符,那么结果将是 NaN
。
const quantity = "awesome";
console.log(Math.floor(quantity));
![Screen-Shot-2022-05-01-at-12.46.08-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.46.08-PM.png)
如何在 JavaScript 中使用 Math.ceil() 函数将一个字符串转换为一个数字
Math.ceil()
函数将把一个数字四舍五入到最接近的整数。
const quantity = "7.18";
console.log(Math.ceil(quantity));
![Screen-Shot-2022-05-01-at-12.48.15-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.48.15-PM.png)
如何在 JavaScript 中使用 Math.round() 函数将一个字符串转换为一个数字
Math.round()
函数将把数字四舍五入为最接近的整数。
如果我的数值是 6.3,那么 Math.round()
将返回6。
const quantity = "6.3";
console.log(Math.round(quantity));
![Screen-Shot-2022-05-01-at-12.50.20-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.50.20-PM.png)
但如果我把这个值改为 6.5,那么 Math.round()
将返回7。
const quantity = "6.5";
console.log(Math.round(quantity));
![Screen-Shot-2022-05-01-at-12.51.35-PM](https://www.freecodecamp.org/news/content/images/2022/05/Screen-Shot-2022-05-01-at-12.51.35-PM.png)
总结
在这篇文章中,我向你展示了用 JavaScript 将字符串转换为数字的 11 种方法。
下面是文章中讨论的 11 种不同的方法:
- 使用
Number()
函数 - 使用
parseInt()
函数 - 使用
parseFloat()
函数 - 使用加号运算符(
+
) - 字符串乘以数字 1
- 字符串除以数字 1
- 从字符串中减去数字 0
- 使用按位非运算符(
~
) - 使用
Math.floor()
函数 - 使用
Math.ceil()
函数 - 使用
Math.round()
函数
我希望你喜欢这篇文章,祝你在 JavaScript 之旅中好运。