Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 851
orokusaki Avatar asked Dec 10 '22 17:12

orokusaki


1 Answers

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
like image 150
miku Avatar answered Dec 28 '22 06:12

miku