gpt4 book ai didi

javascript - 为什么 parseInt(x,0) 与 parseInt(x, 10) 相同?

转载 作者:行者123 更新时间:2023-11-29 16:31:22 25 4
gpt4 key购买 nike

为什么 parseInt('60', 10)parseInt('60', 0) 相同?

JavaScript 将 0 基数转换成什么?

最佳答案

来自 MDN ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt )

If the radix is undefined, 0, or unspecified, JavaScript assumes the following:

If the input string begins with "0x" or "0X" (a zero followed by lowercase or uppercase X), radix is assumed to be 16 and the rest of the string is parsed as a hexidecimal number.

If the input string begins with "0" (a zero), radix is assumed to be 8 (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 clarifies that 10 (decimal) should be used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

If the input string begins with any other value, the radix is 10 (decimal).

因此,如果解析的输入从“0”开始,结果取决于浏览器。

关于javascript - 为什么 parseInt(x,0) 与 parseInt(x, 10) 相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56458990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com