为什么parseInt(8,3)== NaN和parseInt(16,3)== 1? [英] Why is it that parseInt(8,3) == NaN and parseInt(16,3) == 1?

查看:662
本文介绍了为什么parseInt(8,3)== NaN和parseInt(16,3)== 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读,但是我对 parseInt中写的内容感到困惑带有基数的参数

I'm reading this but I'm confused by what is written in the parseInt with a radix argument chapter

为什么是parseInt(8, 3)NaNparseInt(16, 3)1?

AFAIK 8和16不是以3为基数的数字,因此parseInt(16, 3)也应返回NaN

AFAIK 8 and 16 are not base-3 numbers, so parseInt(16, 3) should return NaN too

推荐答案

这是人们一直在旅行的东西,即使他们知道这一点. :-)出于相同的原因,您看到此内容的原因parseInt("1abc")返回1:parseInt在第一个无效字符处停止并返回该点处的所有内容.如果没有要解析的有效字符,则返回NaN.

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. If there are no valid characters to parse, it returns NaN.

parseInt(8, 3)的意思是以基数3解析"8""(请注意,它会将数字8转换为字符串; 一样.由于没有没有个有效字符,因此您得到了NaN.

parseInt(8, 3) means "parse "8" in base 3" (note that it converts the number 8 to a string; details in the spec). But in base 3, the single-digit numbers are just 0, 1, and 2. It's like asking it to parse "9" in octal. Since there were no valid characters, you got NaN.

parseInt(16, 3)要求它在基数3中解析"16".由于它可以解析1,所以可以,然后在6处停止,因为它无法解析.因此它返回1.

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. So it returns 1.

由于这个问题受到了广泛关注,并且在搜索结果中的排名可能很高,因此这里有一些选项,可以将字符串转换为JavaScript中的数字,以及它们的各种特质和应用程序(从我这里的另一个答案中提炼出来) :

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

  • 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. So parseInt("10x") is 10; the x is ignored. Supports an optional radix (number base) argument, so parseInt("15", 16) is 21 (15 in hex). If there's no radix, assumes decimal unless the string starts with 0x (or 0X), in which case it skips those and assumes hex. (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.

parseFloat(str)-与parseInt类似,但是具有浮点数并且仅支持十进制.同样,字符串上的多余字符也将被忽略,因此parseFloat("10.5x")10.5(x被忽略).由于仅支持十进制,因此parseFloat("0x15")0(因为解析在x处结束).如果找不到可解析的数字,则返回NaN.

parseFloat(str) - Like parseInt, but does floating-point numbers and only supports decimal. Again extra characters on the string are ignored, so parseFloat("10.5x") is 10.5 (the x is ignored). As only decimal is supported, parseFloat("0x15") is 0 (because parsing ends at the x). Returns NaN if no parseable digits are found.

一元+,例如+str-(例如,隐式转换)使用浮点和JavaScript的标准数字表示法将 entire 字符串转换为数字(仅数字和小数点=十进制; 0x前缀=十六进制; 0o前缀=八进制[ES2015 +]; 某些实现将其扩展为将前导0视为八进制,但不是在严格模式下). +"10x"NaN,因为被忽略. +"10"10+"10.5"10.5+"0x15"21+"0o10"8 [ES2015 +].有一个陷阱:+""0,而不是您期望的NaN.

Unary +, e.g. +str - (E.g., 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). +"10x" is NaN because the x is not ignored. +"10" is 10, +"10.5" is 10.5, +"0x15" is 21, +"0o10" is 8 [ES2015+]. Has a gotcha: +"" is 0, not NaN as you might expect.

Number(str)-与隐式转换完全相同(例如,与上述一元+一样),但在某些实现中速度较慢. (并非很重要.)

Number(str) - Exactly like implicit conversion (e.g., like the unary + above), but slower on some implementations. (Not that it's likely to matter.)

这篇关于为什么parseInt(8,3)== NaN和parseInt(16,3)== 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆