long(Decimal) performance

ajaksu ajaksu at gmail.com
Fri Aug 11 22:31:45 EDT 2006


Sorry... I'm ashamed to submit such awful code in my first post. Let me
try again...

from decimal import Decimal
def dec2long(number):
    """ Convert decimal.Decimal to long  """
    longstring = str(number)
    if "e" in longstring:
        radix, exponent = longstring.split("e")
    elif "E" in longstring:
        radix, exponent = longstring.split("E")
    else:
        radix, exponent = [longstring, "0"]
    floatexp = long(len(radix.split(".")[1]))
    floatish = Decimal(radix) * 10L**floatexp
    ftol = long(floatish)
    longexp = long(int(exponent) - floatexp)
    return ftol * 10L**longexp

This one should run by itself, is more readable and... still smells bad
:(
Sorry again.




More information about the Python-list mailing list