本文翻译自:Why does parseInt(1/0, 19) return 18?
I have an annoying problem in JavaScript. 我在JavaScript中遇到了烦人的问题。
> parseInt(1 / 0, 19)
> 18
Why does the parseInt
function return 18
? 为什么parseInt
函数返回18
?
#1楼
参考:https://stackoom.com/question/laE5/为什么parseInt-返回
#2楼
The result of 1/0
is Infinity
. 1/0
的结果是Infinity
。
parseInt
treats its first argument as a string which means first of all Infinity.toString()
is called, producing the string "Infinity"
. parseInt
将其第一个参数视为字符串,这意味着首先调用Infinity.toString()
,生成字符串"Infinity"
。 So it works the same as if you asked it to convert "Infinity"
in base 19 to decimal. 因此它的工作原理与您要求它将基数19中的"Infinity"
转换为十进制相同。
Here are the digits in base 19 along with their decimal values: 以下是基数19中的数字及其小数值:
Base 19 Base 10 (decimal)
---------------------------
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
a 10
b 11
c 12
d 13
e 14
f 15
g 16
h 17
i 18
What happens next is that parseInt
scans the input "Infinity"
to find which part of it can be parsed and stops after accepting the first I
(because n
is not a valid digit in base 19). 接下来发生的是parseInt
扫描输入"Infinity"
以找到它的哪个部分可以被解析并在接受第一个I
之后停止(因为n
不是基数19中的有效数字)。
Therefore it behaves as if you called parseInt("I", 19)
, which converts to decimal 18 by the table above. 因此,它的行为就像你调用了parseInt("I", 19)
,它被上面的表转换为十进制18。
#3楼
Here's the sequence of events: 这是事件的顺序:
-
1/0
evaluates toInfinity
1/0
评估为Infinity
-
parseInt
readsInfinity
and happily notes thatI
is 18 in base 19parseInt
读取Infinity
并愉快地注意到I
在19岁时是18岁 -
parseInt
ignores the remainder of the string, since it can't be converted.parseInt
忽略字符串的其余部分,因为它无法转换。
Note that you'd get a result for any base >= 19
, but not for bases below that. 请注意,您将获得任何基数>= 19
,但不会获得低于此值的基数。 For bases >= 24
, you'll get a larger result, as n
becomes a valid digit at that point. 对于>= 24
基数,您将得到更大的结果,因为n
在该点变为有效数字。
#4楼
To add to the above answers: 要添加上述答案:
parseInt is intended to parse strings into numbers (the clue is in the name). parseInt旨在将字符串解析为数字(线索在名称中)。 In your situation, you don't want to do any parsing at all since 1/0 is already a number, so it's a strange choice of function. 在你的情况下,你根本不想做任何解析,因为1/0 已经是一个数字,所以这是一个奇怪的功能选择。 If you have a number (which you do) and want to convert it to a particular base, you should use toString with a radix instead. 如果您有一个数字(您这样做)并希望将其转换为特定基数,则应使用带有基数的toString 。
var num = 1 / 0;
var numInBase19 = num.toString(19); // returns the string "Infinity"
#5楼
To add to the above answers 添加到上面的答案
parseInt(1/0,19)
is equivalent to parseInt("Infinity",19)
parseInt(1/0,19)
相当于parseInt("Infinity",19)
Within base 19 numbers 0-9
and AI
(or ai)
are a valid numbers. 在基数19内,数字0-9
和AI
(or ai)
是有效数字。 So, from the "Infinity" it takes I
of base 19 and converts to base 10 which becomes 18 Then it tries to take the next character ie n
which is not present in base 19 so discards next characters (as per javascript's behavior of converting string to number) 因此,从“Infinity”开始, I
需要基数为19并转换为基数10,然后它变为18然后它尝试获取下一个字符,即基数19中不存在的n
,因此丢弃下一个字符(根据javascript转换字符串的行为)到数字)
So, if you write parseInt("Infinity",19)
OR parseInt("I",19)
OR parseInt("i",19)
the result will be same ie 18
. 因此,如果您编写parseInt("Infinity",19)
或parseInt("I",19)
或parseInt("i",19)
,结果将是相同的,即18
。
Now, if you write parseInt("I0",19)
the result will be 342
as IX 19 (the base)^1 + 0 X 19^0
= 18 X 19^1 + 0 X 19^0
= 18 X 19 + 0 X 1
= 342
现在,如果你写parseInt("I0",19)
,结果将是342
作为IX 19 (the base)^1 + 0 X 19^0
= 18 X 19^1 + 0 X 19^0
= 18 X 19 + 0 X 1
= 342
Similarly, parseInt("I11",19)
will result in 6518
类似地, parseInt("I11",19)
将导致6518
ie 即
18 X 19^2 + 1 X 19^1 + 1 X 19^0
= 18 X 19^2 + 1 X 19^1 + 1 X 19^0
= 18 X 361 + 1 X 19 + 1 X 1
= 6498 + 19 + 1
= 6518