简体   繁体   English

JavaScript 中带前导零的数字

[英]Number with leading zero in JavaScript

Try this:尝试这个:

 var num = 040; console.log(num); // 32

Since when is 40 = 32?从什么时候开始 40 = 32?

With a leading zero, the number is interpreted as octal and 4 * 8 = 32 .使用前导零,该数字被解释为八进制和4 * 8 = 32

TL;DR TL;博士

It's being treated as octal (base 8) because of the leading 0 , just like a leading 0x would make it hex (base 16).由于前导0 ,它被视为八进制(以 8 为底),就像前导0x将使其变为十六进制(以 16 为底)一样。 This has a long and tortured history and is no longer how octal numbers are written in modern JavaScript.这有着悠久而痛苦的历史,不再是现代 JavaScript 中八进制数的书写方式。 In modern JavaScript using strict mode, the "legacy" octal format is a syntax error;在使用严格模式的现代 JavaScript 中,“旧”八进制格式是语法错误; octal numbers are written with an 0o prefix.八进制数以0o前缀书写。

History历史

Early on (in the initial language from Netscape and the first and second ECMAScript specifications), a leading 0 on a numeric literal officially meant octal (base 8), just as a leading 0x means hexadecimal (base 16):早期(在 Netscape 的初始语言以及第一和第二个 ECMAScript 规范中),数字文字上的前导0正式表示八进制(以 8 为基数),就像前导0x表示十六进制(以 16 为基数)一样:

OctalIntegerLiteral ::
    0 OctalDigit
    OctalIntegerLiteral OctalDigit

Eg, 10 , 012 , and 0xA were all ways of writing the decimal number ten.例如, 100120xA都是十进制数十的写法。 This is in keeping with some other languages with syntax similar to JavaScript (C, C++, Java, ...), but it's highly confusing.这与语法类似于 JavaScript (C, C++, Java, ...) 的其他一些语言保持一致,但它非常令人困惑。

As of ECMAScript 3, that form of octal literal was downgraded to an optional extension, and decimal integer literals were changed so that they can't have leading zeros (unless the implementation includes the extension):从 ECMAScript 3 开始,这种形式的八进制文字被降级为可选扩展,并且十进制 integer 文字被更改,因此它们不能有前导零(除非实现包括扩展):

DecimalIntegerLiteral ::
    0
    NonZeroDigit DecimalDigits(opt)

But ECMAScript 5 forbade doing that in strict-mode:但是ECMAScript 5禁止在严格模式下这样做:

A conforming implementation, when processing strict mode code (see 10.1.1) , must not extend the syntax of NumericLiteral to include OctalIntegerLiteral as described in B.1.1 .在处理严格模式代码(参见 10.1.1)时,符合规范的实现不得将NumericLiteral的语法扩展为包括B.1.1中描述的OctalIntegerLiteral

ECMAScript 6 (ECMAScript 2015) introduces BinaryIntegerLiteral and OctalIntegerLiteral , so now we have more coherent literals: ECMAScript 6 (ECMAScript 2015) 引入了BinaryIntegerLiteralOctalIntegerLiteral ,所以现在我们有了更多连贯的文字:

  • BinaryIntegerLiteral , prefixed with 0b or 0B . BinaryIntegerLiteral ,前缀为0b0B
  • OctalIntegerLiteral , prefixed with 0o or 0O . OctalIntegerLiteral ,前缀为0o0O
  • HexIntegerLiteral , prefixed with 0x or 0X . HexIntegerLiteral ,前缀为0x0X

The old OctalIntegerLiteral extension has been renamed to LegacyOctalIntegerLiteral , which is still allowed in non-strict mode.旧的OctalIntegerLiteral扩展已重命名为LegacyOctalIntegerLiteral ,在非严格模式下仍然允许。

Conclusion结论

Therefore, if you want to parse a number in base 8, use the 0o or 0O prefixes (not supported by old browsers), or use parseInt .因此,如果要解析以 8 为底的数字,请使用0o0O前缀(旧浏览器不支持),或使用parseInt

And if you want to be sure your numbers will be parsed in base 10, remove leading zeros, or use parseInt .如果您想确保您的数字将以 10 为底进行解析,请删除前导零,或使用parseInt

Examples例子

  • 010
    • In strict mode (requires ECMAScript 5), it's a syntax error.在严格模式下(需要 ECMAScript 5),这是一个语法错误。
    • In non-strict mode, it may be a syntax error or return 8 (implementation dependent).在非严格模式下,可能是语法错误或返回8 (取决于实现)。
  • 0o10 , 0O10 0o10 , 0O10
    • Before ECMAScript 6, they're syntax errors.在 ECMAScript 6 之前,它们是语法错误。
    • In ECMAScript 6, they return 8 .在 ECMAScript 6 中,它们返回8
  • parseInt('010', 8)
    • It returns 8 .它返回8
  • parseInt('010', 10)
    • It returns 10 .它返回10

If you're interested, you can find the current living specification here , and historical versions here .如果您有兴趣,可以在此处找到当前的生活规范,并此处找到历史版本。

Because the 0 prefix indicates an octal number (base 8).因为0前缀表示八进制数(以 8 为基数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM