什么是非法八进制数字?

发布于 2024-10-20 08:38:10 字数 211 浏览 9 评论 0原文

我正在尝试制作一组​​邮政编码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骷髅 2024-10-27 08:38:11

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"]

杯别 2024-10-27 08:38:11

0 开头的数字假定为八进制格式,就像以 0x 开头的数字假定为十六进制格式一样。八进制数字只能从 07,因此 9 在八进制数字中根本不合法。

最简单的解决方法是简单地以十进制格式编写数字:八进制的 07001 与十进制的 3585 相同(我认为)。或者你的意思是把数字写成十进制?然后,最简单的解决方法是省略前导零:无论如何,070017001 相同。

但是,您提到您需要一组邮政编码。在这种情况下,正确的解决方案是使用邮政编码数组而不是整数数组,因为邮政编码不是整数,它们是邮政编码。

Numbers that start with 0 are assumed to be in octal format, just like numbers that start with 0x are assumed to be in hexadecimal format. Octal digits only go from 0 to 7, so 9 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 as 3585 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 as 7001 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.

独孤求败 2024-10-27 08:38:11

您的数组由数字组成,因此前导零会导致其被解释为八进制(有效数字 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文