This is something people trip over all the time, even when they know about it.(这是人们一直绊倒的事情,即使他们知道这件事。)
:-) You're seeing this for the same reason parseInt("1abc")
returns 1: parseInt
stops at the first invalid character and returns whatever it has at that point.(:-)你看到这个是因为同样的原因parseInt("1abc")
返回1: parseInt
停在第一个无效字符并返回它在那一点上的任何东西。) If there are no valid characters to parse, it returns NaN
.(如果没有要解析的有效字符,则返回NaN
。)
parseInt(8, 3)
means "parse "8"
in base 3" (note that it converts the number 8
to a string; details in the spec ).(parseInt(8, 3)
表示在基数3中“解析"8"
(请注意,它将数字8
转换为字符串; 规范中的详细信息 )。) But in base 3, the single-digit numbers are just 0
, 1
, and 2
.(但是,在基体3中,1位数字只是0
, 1
,和2
。) It's like asking it to parse "9"
in octal.(这就像要求它在八进制中解析"9"
。) Since there were no valid characters, you got NaN
.(由于没有有效字符,你有NaN
。)
parseInt(16, 3)
is asking it to parse "16"
in base 3. Since it can parse the 1
, it does, and then it stops at the 6
because it can't parse it.(parseInt(16, 3)
要求它在基数3中解析"16"
。因为它可以解析1
,它会解析,然后它在6
处停止,因为它无法解析它。) So it returns 1
.(所以它返回1
。)
Since this question is getting a lot of attention and might rank highly in search results, here's a rundown of options for converting strings to numbers in JavaScript, with their various idiosyncracies and applications (lifted from another answer of mine here on SO):(由于这个问题引起了很多关注并且可能在搜索结果中排名很高,所以这里有一个简单的选项,用于将字符串转换为JavaScript中的数字,以及它们的各种特性和应用程序(取决于我的另一个答案):)
parseInt(str[, radix])
- Converts as much of the beginning of the string as it can into a whole (integer) number, ignoring extra characters at the end.(parseInt(str[, radix])
- 尽可能多地将字符串的开头转换为整数(整数),忽略末尾的额外字符。) So parseInt("10x")
is 10
;(所以parseInt("10x")
是10
;) the x
is ignored.(x
被忽略。) Supports an optional radix (number base) argument, so parseInt("15", 16)
is 21
( 15
in hex).(支持可选的基数(数字基数)参数,因此parseInt("15", 16)
为21
(十六进制为15
)。) If there's no radix, assumes decimal unless the string starts with 0x
(or 0X
), in which case it skips those and assumes hex.(如果没有基数,则假定为十进制,除非字符串以0x
(或0X
) 0x
,在这种情况下,它会跳过那些并假定为十六进制。) (Some browsers used to treat strings starting with 0
as octal; that behavior was never specified, and was specifically disallowed in the ES5 specification.) Returns NaN
if no parseable digits are found.((某些浏览器用于将以0
开头的字符串视为八进制;从未指定该行为,并且在ES5规范中明确禁止该行为。)如果未找到可解析的数字,则返回NaN
。)
parseFloat(str)
- Like parseInt
, but does floating-point numbers and only supports decimal.(parseFloat(str)
- 与parseInt
一样,但是浮点数并且只支持十进制数。) Again extra characters on the string are ignored, so parseFloat("10.5x")
is 10.5
(the x
is ignored).(字符串上的额外字符将被忽略,因此parseFloat("10.5x")
为10.5
(忽略x
)。) As only decimal is supported, parseFloat("0x15")
is 0
(because parsing ends at the x
).(由于仅支持十进制,因此parseFloat("0x15")
为0
(因为解析在x
处结束)。) Returns NaN
if no parseable digits are found.(如果找不到可解析的数字,则返回NaN
。)
Unary +
, eg +str
- (Eg, implicit conversion) Converts the entire string to a number using floating point and JavaScript's standard number notation (just digits and a decimal point = decimal; 0x
prefix = hex; 0o
prefix = octal [ES2015+]; some implementations extend it to treat a leading 0
as octal, but not in strict mode).(一元+
,例如+str
- (例如,隐式转换)使用浮点和JavaScript的标准数字表示法将整个字符串转换为数字(仅数字和小数点=十进制; 0x
前缀=十六进制; 0o
前缀=八进制[ES2015 +] ; 一些实现扩展它以将前导0
视为八进制,但不是在严格模式下。) +"10x"
is NaN
because the x
is not ignored.(+"10x"
是NaN
因为x
不被忽略。) +"10"
is 10
, +"10.5"
is 10.5
, +"0x15"
is 21
, +"0o10"
is 8
[ES2015+].(+"10"
为10
, +"10.5"
为10.5
, +"0x15"
为21
, +"0o10"
为8
[ES2015 +]。) Has a gotcha: +""
is 0
, not NaN
as you might expect.(有一个问题: +""
是0
,而不是你NaN
。)
Number(str)
- Exactly like implicit conversion (eg, like the unary +
above), but slower on some implementations.(Number(str)
- 完全像隐式转换(例如,像上面的一元+
),但在某些实现上更慢。) (Not that it's likely to matter.)((不是说它很重要。))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…