如何将 000000000001 变成 1?
我需要将格式化整数转换为常规整数:
- 000000000001 需要转换为 1
- 000000000053 需要转换为 53
- 000000965948 需要转换为 965948
等等。
看起来一个简单的 int(000000000015)
结果就是数字 13。我知道幕后有一些奇怪的东西。每次都能准确地做到这一点的最佳方法是什么?
I need to turn a formatted integer into a regular integer:
- 000000000001 needs to be turned into 1
- 000000000053 needs to be turned into 53
- 000000965948 needs to be turned into 965948
And so on.
It seems that a simple int(000000000015)
results in the number 13. I understand there is some weird stuff behind the scenes. What is the best way to do this accurately every time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以
0
开头的数字被视为八进制。您可以将零填充数字包装成字符串,然后它应该可以工作。
int()
接受一个可选参数,它是基础,因此上面的内容相当于:Numbers starting with
0
are considered octal.You can wrap your zero-padded number into a string, then it should work.
int()
takes an optional argument, which is the base, so the above would be the equivalent of:前导零被视为八进制。我假设您正在转换字符串“000000013”,而不是文字 000000013,因此您应该能够通过
int("000000013",10)
将它们转换为基数 10 整数。已经完成”,并且它们是文字,已经转换为八进制文字,您可以使用以下内容(注意,这是有害的和异端邪说:)
The leading zeroes are considered octal. I am assuming that you are converting the string "000000013", not literal 000000013, so you should be able to convert these to base 10 integer by
int("000000013",10)
If the "harm has already been done", and they are literals, that have already been converted to octal literals, you can use the following (beware, this is harmful and heresy:)
试试这个:
Try this: