什么是非法八进制数字?
我正在尝试制作一组邮政编码。
array = [07001, 07920]
这返回:
array = [07001, 07920]
^
from (irb):12
from :0
以前从未见过这个。有什么解决方法吗?
I'm trying to make an array of zip codes.
array = [07001, 07920]
This returns :
array = [07001, 07920]
^
from (irb):12
from :0
Never seen this before. Any workarounds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ruby 将前导 0 的数字解释为八进制(基数 8)。因此数字 8 和 9 无效。
将邮政编码存储为字符串而不是数字可能更有意义(以避免在显示时必须用零填充),例如:
array = ["07001", "07920"]
Ruby is interpreting numbers that have a leading 0 as being in octal (base 8). Thus the digits 8 and 9 are not valid.
It probably makes more sense to store ZIP codes as strings, instead of as numbers (to avoid having to pad with zeroes whenever you display it), as such:
array = ["07001", "07920"]
以
0
开头的数字假定为八进制格式,就像以0x
开头的数字假定为十六进制格式一样。八进制数字只能从0
到7
,因此9
在八进制数字中根本不合法。最简单的解决方法是简单地以十进制格式编写数字:八进制的
07001
与十进制的3585
相同(我认为)。或者你的意思是把数字写成十进制?然后,最简单的解决方法是省略前导零:无论如何,07001
与7001
相同。但是,您提到您需要一组邮政编码。在这种情况下,正确的解决方案是使用邮政编码数组而不是整数数组,因为邮政编码不是整数,它们是邮政编码。
Numbers that start with
0
are assumed to be in octal format, just like numbers that start with0x
are assumed to be in hexadecimal format. Octal digits only go from0
to7
, so9
is simply not legal in an octal number.The easiest workaround would be to simply write the numbers in decimal format:
07001
in octal is the same as3585
in decimal (I think). Or did you mean to write the numbers in decimal? Then, the easiest workaround is to leave off the leading zeroes:07001
is the same as7001
anyway.However, you mention that you want an array of ZIP codes. In that case, the correct solution would be to use, well, an array of ZIP codes instead of an array of integers, since ZIP codes aren't integers, they are ZIP codes.
您的数组由数字组成,因此前导零会导致其被解释为八进制(有效数字 0-7)。如果这些是邮政编码,并且前导零很重要,那么它们可能应该是字符串。
Your array is of numbers, so the leading zero causes it to be interpreted as octal (valid digits 0-7). If these are zip codes, and the leading zero is significant, they should probably be strings.