简体   繁体   English

如何将以零开头的十进制数转换为字符串

[英]How to turn a base ten Number with leading zeros into a string

Is there any way to turn a base ten number with leading zeroes into a string in javascript? 有什么办法可以将以前导零开头的十进制数转换为javascript中的字符串? It seems as if javascript just doesn't have the concept of a base ten number with leading zeroes. 似乎javascript只是没有以十进制数开头的零的概念。

Example of what I want 我想要的例子

function turnBaseTenNumberToString(num) {
  ...
}

console.log(turnBaseTenNumberToString(010));
// outputs
// '010'

I should note the following does not work 我应该注意以下内容不起作用
Number.prototype.toString(<radix>) Number.prototype.toString(<基数>)

console.log((010).toString(10))
// outputs
// '8'

which I assume is the result of the number evaluating prior to calling toString. 我认为这是调用toString之前对数字求值的结果。

The problem is that decimal integer literals can't have leading zeros: 问题在于十进制整数文字不能包含前导零:

DecimalIntegerLiteral ::
    0
    NonZeroDigit DecimalDigits(opt)

However, ECMAScript 3 allowed (as an optional extension) to parse literals with leading zeros in base 8: 但是,ECMAScript 3允许(作为可选扩展)解析以8为底的前导零的文字:

OctalIntegerLiteral ::
    0 OctalDigit
    OctalIntegerLiteral OctalDigit

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的语法,以包括OctalIntegerLiteral如描述B.1.1

ECMAScript 6 introduces BinaryIntegerLiteral and OctalIntegerLiteral , so now we have more coherent literals: ECMAScript 6引入了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 ,但仍可以在非严格模式下使用。

Therefore, 010 is a syntax error, which may be tolerated by some browsers only in non-strict mode. 因此, 010是语法错误,某些浏览器仅在非严格模式下可以容忍该错误。 But you can't rely on that, and even then it will produce the number 8 . 但是您不能依靠它,即使那样它也会产生数字8

As per OP 根据OP

turn a base ten number with leading zeroes into a string 将以零开头的十进制数转换为字符串

Well, JavaScript treats a leading zero as an indicator that the value is in base 8. Thus when defining number as 010 the number value in decimal system is 8 (1 * 8^1 + 0 * 8^0) . 好吧,JavaScript将前导零视为该值以8为底的指示符。因此,将数字定义为010 ,十进制系统中的数字值为8 (1 * 8^1 + 0 * 8^0)

If you want to avoid that, you should pass a number in string representation and remove a leading zero if needed. 如果要避免这种情况,则应以字符串表示形式传递一个数字,并在需要时删除前导零。

 function turnBaseTenStringToNumber(num) { return parseInt(num.replace(/^0+/, '')); } console.log(turnBaseTenStringToNumber(`010`)); // outputs 10 

See number with radix documentation for more details. 有关更多详细信息,请参见带有基数文档的编号。

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

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