为什么在 JavaScript 中是 032*2 = 52?
Why 032*2 = 52 in JavaScript?
我很想知道为什么 032*2
returns 52
在 JavaScript.
我觉得 032
可以作为 octal notation
相互渗透,但我找不到合适的引用。
请问:
- 给我一个解释和参考。
在此先感谢您的帮助。
以0
开头的Numbers是八进制。所以,032 === 26
.
要将其转换为 Base-10/十进制 数字使用 parseInt
和 radix 10.
parseInt('032', 10) * 2; // 64
来自 MDN 文档:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
- 如果输入字符串以“0x”或“0X”开头,则基数为 16(十六进制)并解析字符串的其余部分。
- 如果输入字符串以“0”开头,则基数为八(八进制)或 10(十进制)。具体选择哪个基数取决于实现。 ECMAScript 5 指定使用 10(十进制),但并非所有浏览器都支持。因此,在使用 parseInt 时总是指定基数。
- 如果输入字符串以任何其他值开头,则基数为 10(十进制)。
Octal interpretations with no radix
Although discouraged by ECMAScript 3 and forbidden by ECMAScript 5,
many implementations interpret a numeric string beginning with a leading 0 as octal.The following may have an octal result, or it may have a decimal result.
Always specify a radix to avoid this unreliable behavior.
来源:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/parseInt
ECMAScript 5.1 指定了对数字文字的一些扩展 in its annex:
The syntax and semantics of 7.8.3 [Numeric literals] can be extended as follows except that this extension is not allowed for strict mode code:
NumericLiteral ::
DecimalLiteral
HexIntegerLiteral
OctalIntegerLiteral
OctalIntegerLiteral ::
0 OctalDigit
OctalIntegerLiteral OctalDigit
由此得出结论,任何以 0
开头且后跟 OctalDigit
的数字都可以被视为八进制数字。但是,这在严格模式下是不允许的。
我很想知道为什么 032*2
returns 52
在 JavaScript.
我觉得 032
可以作为 octal notation
相互渗透,但我找不到合适的引用。
请问:
- 给我一个解释和参考。
在此先感谢您的帮助。
0
开头的Numbers是八进制。所以,032 === 26
.
要将其转换为 Base-10/十进制 数字使用 parseInt
和 radix 10.
parseInt('032', 10) * 2; // 64
来自 MDN 文档:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
- 如果输入字符串以“0x”或“0X”开头,则基数为 16(十六进制)并解析字符串的其余部分。
- 如果输入字符串以“0”开头,则基数为八(八进制)或 10(十进制)。具体选择哪个基数取决于实现。 ECMAScript 5 指定使用 10(十进制),但并非所有浏览器都支持。因此,在使用 parseInt 时总是指定基数。
- 如果输入字符串以任何其他值开头,则基数为 10(十进制)。
Octal interpretations with no radix
Although discouraged by ECMAScript 3 and forbidden by ECMAScript 5, many implementations interpret a numeric string beginning with a leading 0 as octal.The following may have an octal result, or it may have a decimal result.
Always specify a radix to avoid this unreliable behavior.
来源:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/parseInt
ECMAScript 5.1 指定了对数字文字的一些扩展 in its annex:
The syntax and semantics of 7.8.3 [Numeric literals] can be extended as follows except that this extension is not allowed for strict mode code:
NumericLiteral :: DecimalLiteral HexIntegerLiteral OctalIntegerLiteral OctalIntegerLiteral :: 0 OctalDigit OctalIntegerLiteral OctalDigit
由此得出结论,任何以 0
开头且后跟 OctalDigit
的数字都可以被视为八进制数字。但是,这在严格模式下是不允许的。