带有前导零的 Javascript parseInt()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8763396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript parseInt() with leading zeros
提问by savinger
Javascript's parseInt function does not seem to completely work.
Javascript 的 parseInt 函数似乎不能完全工作。
parseInt("01") returns 1
parseInt("02") returns 2
parseInt("03") returns 3
parseInt("04") returns 4
parseInt("05") returns 5
parseInt("06") returns 6
parseInt("07") returns 7
parseInt("08") returns 0
parseInt("09") returns 0
You can't explain that. Give it a try. (jsFiddle)
你无法解释。试一试。(jsFiddle)
EditSince this question was asked and answered, the "feature" of defaulting to octal radix has been deprecated. [1] [2]
回答by Rocket Hazmat
This is because if a number starts with a '0', it's treated as base 8 (octal).
这是因为如果一个数字以“0”开头,则它被视为基数为 8(八进制)。
You can force the base by passing the base as the 2nd parameter.
您可以通过将基数作为第二个参数传递来强制基数。
parseInt("09", 10) // 9
According to the docs, the 2nd parameter is optional, but it's not alwaysassumed to be 10, as you can see from your example.
根据docs,第二个参数是可选的,但并不总是假设为 10,正如您从示例中看到的那样。
回答by Wayne
Calls to parseInt
should always specify a base in the second argument:
调用parseInt
应始终在第二个参数中指定一个基数:
parseInt("08", 10);
Earlier versions of JavaScript treat strings starting with 0
as octal (when no base is specified) and neither 08
nor 09
are valid octal numbers.
早期版本的 JavaScript 将字符串以0
八进制开头(当未指定基数时),并且既不是有效的八进制数,08
也不09
是有效的八进制数。
From the Mozilla documentation:
从 Mozilla文档:
If radix is undefined or 0, JavaScript assumes the following:
- If the input string begins with "0x" or "0X", radix is 16 (hexadecimal).
- If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.
- If the input string begins with any other value, the radix is 10 (decimal).
If the first character cannot be converted to a number, parseInt returns NaN.
如果 radix 未定义或为 0,则 JavaScript 假定如下:
- 如果输入字符串以“0x”或“0X”开头,则基数为 16(十六进制)。
- 如果输入字符串以“0”开头,则基数为八(八进制)。这个特性是非标准的,一些实现故意不支持它(而是使用基数 10)。由于这个原因,在使用 parseInt 时总是指定一个基数。
- 如果输入字符串以任何其他值开头,则基数为 10(十进制)。
如果第一个字符无法转换为数字,则 parseInt 返回 NaN。
And from the ECMAScript 3standard:
并且来自ECMAScript 3标准:
When radix is 0 or undefinedand the string's number begins with a 0digit not followed by an xor X, then the implementation may, at its discretion, interpret the number either as being octal or as being decimal. Implementations are encouraged to interpret numbers in this case as being decimal.
当基数为 0 或未定义并且字符串的数字以0数字开头,后面没有x或X 时,实现可以自行决定将数字解释为八进制或十进制。鼓励实现将这种情况下的数字解释为十进制。
The latest version of JavaScript (ECMAScript 5) abandons this behavior, but you should still specify the radixto satisfy older browsers.
最新版本的 JavaScript ( ECMAScript 5)放弃了这种行为,但您仍应指定基数以满足旧浏览器的要求。
回答by John Hartsock
There is a Radix parameter
有一个基数参数
parseInt(value, base)
Where base is the radix.
其中 base 是基数。
In this case you are evaluating base10 (decimal) numbers, therefore use
在这种情况下,您正在评估 base10(十进制)数字,因此使用
parseInt(value, 10);
回答by Maffelu
This doesn't seem to be completely valid in new browsers. Internet Explorer 9 and 10 will return 8 if you execute 'parseInt("08")' whereas Internet Explorer 8 and previous versions will return 0 (IE10 in quirks mode will also return 0).
这在新浏览器中似乎并不完全有效。如果您执行 'parseInt("08")',Internet Explorer 9 和 10 将返回 8,而 Internet Explorer 8 和以前的版本将返回 0(在 quirks 模式下的 IE10 也将返回 0)。
The latest version of Chrome also returns 8 so they must have changed the interpretation recently.
最新版本的 Chrome 也返回 8 所以他们最近肯定改变了解释。
回答by Muhammad Imran
This issue is deprecated now. But you can still use radix in parseInt
to convert number of other bases into base-10. E.g.,
此问题现已弃用。但是您仍然可以使用基数 inparseInt
将其他基数转换为基数 10。例如,
var baseTwoNumber = parseInt('0010001', 2);
returns 17
(which is base-10 of 0010001
).
返回17
(以 10 为底0010001
)。
回答by mythicalcoder
The issue seems to have changed now in most browsers.
在大多数浏览器中,这个问题现在似乎已经改变了。
Firefox 51.0.1 (64-bit)
火狐 51.0.1(64 位)
parseInt("09") // 9
Chrome 55.0.2883.95 (64-bit)
Chrome 55.0.2883.95(64 位)
parseInt("09") // 9
Safari 10.0 (12602.1.50.0.10)
Safari 10.0 (12602.1.50.0.10)
parseInt("09") // 9
=====
======
Recommended Practice
推荐做法
Having said that, just to be on the safer side and to avoid issues, use the base/radix parameter as suggested in the accepted answer.
话虽如此,为了更安全并避免出现问题,请按照已接受的答案中的建议使用 base/radix 参数。
parseInt("09", 10) // 9
Extra test
额外测试
I just wanted to test this as well, if the argument is not a string. Chrome & Safari gives exact result. Firefox too returns proper result, but with a warning.
如果参数不是字符串,我也只是想测试一下。Chrome & Safari 给出了准确的结果。Firefox 也返回正确的结果,但有警告。
parseInt(09) // 9. (Warning: SyntaxError: 09 is not a legal ECMA-262 octal constant)
回答by Endless
Tip: As you now know when default to octal has been deprecated. Here is how you would fix it in legacy browsers
提示:正如您现在知道的,默认八进制已被弃用。这是在旧版浏览器中修复它的方法
// ES-5 15.1.2.2
if (parseInt('08') !== 8 || parseInt('0x16') !== 22) {
parseInt = (function (origParseInt) {
var hexRegex = /^0[xX]/;
return function parseIntES5(str, radix) {
str = String(str).trim();
if (!Number(radix)) {
radix = hexRegex.test(str) ? 16 : 10;
}
return origParseInt(str, radix);
};
}(parseInt));
}