Javascript 为什么 parseInt(1/0, 19) 返回 18?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11340673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does parseInt(1/0, 19) return 18?
提问by cebor
I have an annoying problem in JavaScript.
我在JavaScript 中有一个烦人的问题。
> parseInt(1 / 0, 19)
> 18
Why does the parseInt
function return 18
?
为什么parseInt
函数返回18
?
回答by Jon
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"
. So it works the same as if you asked it to convert "Infinity"
in base 19 to decimal.
parseInt
将其第一个参数视为字符串,这意味着首先Infinity.toString()
被调用,生成字符串"Infinity"
。所以它的工作原理与您要求它以"Infinity"
19 进制转换为十进制的方式相同。
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。
回答by Craig Citro
Here's the sequence of events:
这是事件的顺序:
1/0
evaluates toInfinity
parseInt
readsInfinity
and happily notes thatI
is 18 in base 19parseInt
ignores the remainder of the string, since it can't be converted.
1/0
评估为Infinity
parseInt
阅读Infinity
并愉快地注意到I
以 19 为底的 18parseInt
忽略字符串的其余部分,因为它不能被转换。
Note that you'd get a result for any base >= 19
, but not for bases below that. For bases >= 24
, you'll get a larger result, as n
becomes a valid digit at that point.
请注意,您会得到任何 base 的结果>= 19
,但不会得到低于该值的基数。对于 bases >= 24
,您将获得更大的结果,因为此时n
成为有效数字。
回答by kybernetikos
To add to the above answers:
要添加到上述答案:
parseInt is intended to parse strings into numbers (the clue is in the name). In your situation, you don't want to do any parsing at all since 1/0 is alreadya number, so it's a strange choice of function. If you have a number (which you do) and want to convert it to a particular base, you should use toString with a radixinstead.
parseInt 旨在将字符串解析为数字(线索在名称中)。在您的情况下,您根本不想进行任何解析,因为 1/0已经是一个数字,因此这是一个奇怪的函数选择。如果您有一个数字(您这样做)并希望将其转换为特定的基数,则应使用带有基数的 toString代替。
var num = 1 / 0;
var numInBase19 = num.toString(19); // returns the string "Infinity"
回答by Imdad
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 A-I
(or a-i)
are a valid numbers. 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 i.e. n
which is not present in base 19 so discards next characters (as per javascript's behavior of converting string to number)
在基数为 19 的数字内,0-9
并且 A-I
(or a-i)
是有效数字。因此,从“无穷大”它需要I
基数 19 并转换为基数 10 变为 18 然后它尝试采用下一个字符,即n
基数 19 中不存在的字符,因此丢弃下一个字符(根据 javascript 将字符串转换为数字的行为) )
So, if you write parseInt("Infinity",19)
OR parseInt("I",19)
OR parseInt("i",19)
the result will be same i.e 18
.
所以,如果你写parseInt("Infinity",19)
OR parseInt("I",19)
ORparseInt("i",19)
结果将是相同的,即18
.
Now, if you write parseInt("I0",19)
the result will be 342
as I X 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
为I X 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
i.e.
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