JavaScript函数parseInt()不能正确解析前导0的数字 [英] JavaScript function parseInt() doesn't parse numbers with leading 0 correctly
问题描述
我在正整数之前有一些零。我想删除零,所以只剩下正整数。像'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屋!