JavaScript函数parseInt()不能正确解析前导0的数字 [英] JavaScript function parseInt() doesn't parse numbers with leading 0 correctly

查看:151
本文介绍了JavaScript函数parseInt()不能正确解析前导0的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正整数之前有一些零。我想删除零,所以只剩下正整数。像'001'只会是'1'。我认为最简单的方法是使用parseInt('001')。但我发现它不适用于数字8和9.示例parseInt('008')将导致'0'而不是'8'。

I have some zeros prior to a positive integer. I want to remove the zeros so only the positive integer remains. Like '001' will only be '1'. I thought the easiest way was to use parseInt('001'). But what I discovered is that it don't works for the number 8 and 9. Example parseInt('008') will result in '0' instead of '8'.

以下是整个HTML代码:

Here are the whole html code:

<html> <body>
<script>
var integer = parseInt('002');
document.write(integer);

</script>
</body> </html>

但我可以以某种方式报告此问题吗?有人知道另一个简单的解决方法吗?

But can I somehow report this problem? Do anyone know an another easy workaround this problem?

推荐答案

这是记录在案的行为: http://www.w3schools.com/jsref/jsref_parseInt.asp

This is documented behavior: http://www.w3schools.com/jsref/jsref_parseInt.asp

前导'0'的字符串被解析为好像是八进制。

Strings with a leading '0' are parsed as if they were octal.

这篇关于JavaScript函数parseInt()不能正确解析前导0的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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