gpt4 book ai didi

javascript - 为什么 parseInt 不能处理带有前导 0 的大数?

转载 作者:行者123 更新时间:2023-11-28 16:02:53 25 4
gpt4 key购买 nike

avar = "0000013482000000";
t = parseInt(avar);

当我运行它时,t92因为某些原因。如果我删除前导 0,那么它就可以正常工作。为什么会这样?

最佳答案

试试这个:

avar = "0000013482000000";
t = parseInt(avar,10);

如果字符串以 0 开头,某些浏览器可能会认为它是八进制数。

但是,在具有新 ECMAScript 标准的现代浏览器中,,10 不是必需的,因为除非指定或以 0x(十六进制)开头,否则它们始终被视为十进制。

enter image description here

Chrome 是默认基数为 10 的浏览器之一。

引用:ECMAScript Language Specification第104页

The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed. If radix is 16, the number may also optionally begin with the character pairs 0x or 0X.

关于javascript - 为什么 parseInt 不能处理带有前导 0 的大数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16291324/

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