我目前正在编写一段使用 Base 36 编码的 JavaScript。
我遇到了这个问题:
parseInt("welcomeback",36).toString(36)
看来要回归了"welcomebacg"
.
我在 Chrome 开发者控制台和 Node.js 中对此进行了测试,结果相同。
这个结果有什么合乎逻辑的解释吗?
The result of parseInt("welcomeback",36)
is larger than Number.MAX_SAFE_INTEGER https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER (253-1) and thus cannot be accurately represented. A possible workaround is to perform base conversion with BigInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt manually.
const str = "welcomeback";
const base = 36;
const res = [...str].reduce((acc,curr)=>
BigInt(parseInt(curr, base)) + BigInt(base) * acc, 0n);
console.log(res.toString());
console.log(res.toString(36));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)