Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - 为什么parseInt(1 / 0,19)返回18?(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 ?)   ask by cebor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...