如何在JavaScript中将字符串转换为数字(包括示例)

将字符串转换为数字 (Converting Strings to Numbers)

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

parseInt()函数解析字符串参数,并返回指定基数(数学数字系统中的基数)的整数。

parseInt(string, radix);

参量 (Parameters)

string

The value to parse. If the string argument is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string argument is ignored.’= radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10. Return value An integer number parsed from the given string. If the first character cannot be converted to a number, NaN is returned.

要解析的值。 如果string参数不是字符串,则将其转换为字符串(使用ToString抽象操作)。 字符串参数中的前导空格将被忽略。'= radix 2到36之间的整数,表示上述字符串的基数(数学数字系统中的基数)。 为人类常用的十进制系统指定10 。 始终指定此参数,以消除读者的困惑并确保可预测的行为。 当未指定基数时,不同的实现产生不同的结果,通常将其默认值设置为10。返回值从给定字符串中解析的整数。 如果第一个字符不能转换为数字,则返回NaN

描述 (Description)

The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the integer that is the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16),A through F is used.

parseInt函数将其第一个参数转换为字符串,对其进行解析,然后返回整数或NaN 。 如果不是NaN ,则返回的值将是整数,该整数是指定基数(基数)中作为数字的第一个参数。 例如,基数10表示要从十进制数,八进制八进制,十六进制十六进制等进行转换。 对于半径大于10的字母,字母表示大于9的数字。例如,对于十六进制数字(以16为底),使用AF

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

如果parseInt遇到的字符不是指定基数中的数字,则它将忽略该字符以及所有后续字符,并返回到该点为止已解析的整数值。 parseInt将数字截断为整数值。 允许前导和尾随空格。

Because some numbers include the e character in their string representation (e.g. 6.022e23), using parseInt to truncate numeric values will produce unexpected results when used on very large or very small numbers. parseInt should not be used as a substitute for Math.floor().

由于某些数字在其字符串表示形式中包含e字符(例如6.022e23 ),因此,当对非常大或非常小的数字使用数字时,使用parseInt截断数字值将产生意外结果。 parseInt不应替代Math.floor()

If radix is undefined or 0 (or absent), JavaScript assumes the following:

如果radix undefined或为0(或不存在),则JavaScript假定以下内容:

  • If the input string begins with “0x” or “0X”, radix is 16 (hexadecimal) and the remainder of the string is parsed.

    如果输入string以“ 0x”或“ 0X”开头,则基数为16(十六进制),并分析字符串的其余部分。

  • If the input string begins with “0”, radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason, always specify a radix when using parseInt.

    如果输入string以“ 0”开头,则基数为8(八进制)或10(十进制)。 选择哪个基数取决于实现。 ECMAScript 5指定使用10(十进制),但并非所有浏览器都支持。 因此,在使用parseInt时,请始终指定基数。

  • If the input string begins with any other value, the radix is 10 (decimal).

    如果输入string以任何其他值开头,则基数为10(十进制)。

  • If the first character cannot be converted to a number, parseInt returns NaN.

    如果第一个字符不能转换为数字,则parseInt返回NaN。

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

出于算术目的,NaN值不是任何基数的数字。 您可以调用isNaN函数来确定parseInt的结果是否为NaN。 如果将NaN传递给算术运算,则运算结果也将为NaN。

To convert the number to its string literal in a particular radix use intValue.toString(radix).

要将数字转换为特定基数的字符串文字,请使用intValue.toString(radix)。

例子 (Examples)

Using parseInt The following examples all return 15:

使用parseInt以下示例全部返回15

parseInt(' 0xF', 16);
parseInt(' F', 16);
parseInt('17', 8);
parseInt(021, 8);
parseInt('015', 10);   // parseInt(015, 10); will return 15
parseInt(15.99, 10);
parseInt('15,123', 10);
parseInt('FXX123', 16);
parseInt('1111', 2);
parseInt('15 * 3', 10);
parseInt('15e2', 10);
parseInt('15px', 10);
parseInt('12', 13);

The following examples all return NaN:

以下示例均返回NaN

parseInt('Hello', 8); // Not a number at all
parseInt('546', 2);   // Digits are not valid for binary representations

The following examples all return -15:

以下示例均返回-15

parseInt('-F', 16);
parseInt('-0F', 16);
parseInt('-0XF', 16);
parseInt(-15.1, 10)
parseInt(' -17', 8);
parseInt(' -15', 10);
parseInt('-1111', 2);
parseInt('-15e1', 10);
parseInt('-12', 13);

The following examples all return 4:

以下示例均返回4

parseInt(4.7, 10);
parseInt(4.7 * 1e22, 10); // Very large number becomes 4
parseInt(0.00000000000434, 10); // Very small number becomes 4

The following example returns 224:

以下示例返回224

parseInt('0e0', 16);
更多信息: (More Information:)

parseInt on MDN

在MDN上解析

  • parseInt() and parseFloat() attempt to convert the string to a number if possible. For example, var x = parseInt("100"); // x = 100

    如果可能, parseInt()parseFloat()尝试将字符串转换为数字。 例如, var x = parseInt("100"); // x = 100 var x = parseInt("100"); // x = 100

  • Number() will convert to a number the value can be represented by. This includes dates into the number of milliseconds since midnight January 1, 1970 UTC, boolean values to 1 or 0, and values that can’t be converted to a recognisable number will become NaN. That stands for Not a Number and is also technically a number!

    Number()将转换为可以表示值的数字。 其中包括日期,以自UTC 1970年1月1日午夜以来的毫秒数为单位,布尔值设置为1或0,并且无法转换为可识别数字的值将变为NaN。 那代表不是数字,从技术上讲也是数字!

翻译自: https://www.freecodecamp.org/news/convert-string-to-number-javascript/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值