为什么parseInt('08')给出0,而parseInt('07')给出7 [英] why parseInt('08') is giving 0, whereas parseInt('07') is giving 7

查看:157
本文介绍了为什么parseInt('08')给出0,而parseInt('07')给出7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JavaScript parseInt八进制bug的变通办法

我是在javascript上工作​​,我似乎发现这很奇怪,javascript函数parseInt('08')返回0而parseInt('07')返回7。

I am working on javascript, and I seem to find this strange, that the javascript function parseInt('08') is returning 0 and parseInt('07') is returning 7.

这种行为似乎存在于Firefox中。

this behavior seems to be there in Firefox.

parseInt('08')在IE中返回8,但在Firefox中返回0 ..

parseInt('08') is returning 8 in IE, but 0 in Firefox..

为什么?我希望parseInt('08')按预期返回8并进入IE。

Why? I want parseInt('08') to return 8, as expected and getting in IE.

推荐答案

是的,我遇到过这个一个之前。这真的很奇怪,因为有些浏览器会将其解释为您希望在基数8中解析它。请考虑以下文章:

Yeah, I came across this one before. It is really odd because some browsers interpret this as you wanting to parse it in base 8. Consider the following article:

http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604

基本上,你必须告诉解析器使用10号基数:

basically, you have to tell the parser to use base 10 numbers:

parseInt('08', '10');

这篇关于为什么parseInt('08')给出0,而parseInt('07')给出7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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