开发者

How can I turn 000000000001 into 1?

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 s开发者_开发百科eems 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?


Numbers starting with 0 are considered octal.

>>> 07
7
>>> 08
   File "<stdin>", line 1
   08
    ^
SyntaxError: invalid token

You can wrap your zero-padded number into a string, then it should work.

>>> int("08")
8

int() takes an optional argument, which is the base, so the above would be the equivalent of:

>>> int("08", 10)
8


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:)

int(("%o" % 00000013),10)


Try this:

>>> int('00000000053', 10)
53


if x = "0000000000000001":
    x = 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜