base36 - Python: converting base 10 to base 36 -


i'm trying convert decimal base 36 (...8,9,a,b,c...x,y,z,10,11...) when run code bunch of floats instead of integers.

def trc(n):     if (n < 0): print(0, end='')     elif (n<=1): print(n, end='')     else:         trc( n / 36 )         x =(n%36)         if (x < 10): print(x, end='')         else: print(chr(x+87), end='') 

i based code off of this.

in python 3, / operator floating point division, when both arguments integers. change python 2, dividing 2 integers discard fractional part.

you can explicitly request integer division using // operator. result rounded towards negative infinity. or, since you're calculating modulus, use divmod them both @ same time:

else:     n, x = divmod(n, 36)     trc(n)     if x < 10: # ... 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -