为什么JavaScript的parseInt的基数默认为8? [英] Why does the radix for JavaScript's parseInt default to 8?

查看:341
本文介绍了为什么JavaScript的parseInt的基数默认为8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript的parseInt函数中将基数默认为8(如果字符串以0开头)使我烦恼,只因为我继续忘记将可选的第二个参数传递为10.我正在寻找答案告诉我为什么它将默认值设置为8是有意义的。

Defaulting the radix to 8 (if the string starts with a 0) in JavaScript's parseInt function annoys me, only because I continue to forgot to pass the optional second argument as 10. I'm looking for an answer telling me why it makes sense to have it default to 8.

推荐答案

如果输入字符串以0开头,它只会默认为8。来自C和C ++是一个不幸的结转。

It only "defaults" to 8 if the input string starts with 0. This is an unfortunate carryover from C and C++.

您可以使用数字('0123')来代替,或者,正如你在问题中所说, parseInt('0123',10)

You can use Number('0123') instead, or, as you said in the question, parseInt('0123', 10).

如何解决JavaScript的parseInt八进制行为?


您能告诉我更多有关此结转的信息吗?

Can you tell me more about this carryover?



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆