Skip to content Skip to sidebar Skip to footer

Not Getting Exact Result In Python With The Values Leading Zero. Please Tell Me What Is Going On There

zipcode = 02132 print zipcode result = 1114

Solution 1:

A leading zero means octal. 2132 in octal equals 1114 in decimal. They removed this behavior in Python 3.0.


Solution 2:

In Python 2.x, number with a leading zero is interpreted as octal (base-eight). Python 3.x requires a leading "0o" to indicate an octal number. You probably want to treat a zipcode as a string to keep the leading zeroes intact.


Solution 3:

Quite apart from the octal caper:

Zip codes, social security "numbers", credit card "numbers", phone "numbers", etc are NOT numbers in the sense that you can do meaningful arithmetic on them, so don't keep them as integers, keep them as strings.


Solution 4:

The leading 0 makes it assume 02132 is octal.


Solution 5:

leading zero means octal as other have said. one way to keep your zero is to strip the leading zeros and just use a zero padded string when you display it,


>>> myInt = 2132
>>> print myInt
2132
>>> myString = "%05d" % myInt
>>> print myString
02132
>>> print int(myString)
2132

you probably get the idea.


Post a Comment for "Not Getting Exact Result In Python With The Values Leading Zero. Please Tell Me What Is Going On There"