为什么JavaScript的parseInt的基数默认为8? [英] Why does the radix for JavaScript's parseInt default to 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)
.
您能告诉我更多有关此结转的信息吗?
Can you tell me more about this carryover?
- Ja vascript eval函数返回八进制值
- 八进制数字文字:什么时候?为什么?曾经?
- Javascript eval function returning Octal value
- Octal number literals: When? Why? Ever?
这篇关于为什么JavaScript的parseInt的基数默认为8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!