force non-exponential representation for Decimal?

jholg at gmx.de jholg at gmx.de
Wed Dec 23 05:03:58 EST 2009


(cc-ing the list)

> > Is there a convenient way to force a decimal.Decimal representation to
> not use exponential representation?
> 
> Which Python version are you using?  For Python 2.6 (and 3.1), the
> answer's yes.  For earlier Python verions, I don't think so.  In
> Python 2.6, use new-style formatting with the 'f' modifier:
> 
> >>> '{0:f}'.format(Decimal('1e-100'))
> '0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001'
> >>> '{0:f}'.format(Decimal('1e100'))
> '10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

Unfortunately, I'm still using Python 2.4 so I can't go that way (at least not anytime soon, this is a production environment).

> For earlier Python versions, you can manipulate the output string as
> you describe, or you can extract the raw components of the Decimal
> instance (e.g. with the as_tuple() method) and construct a string
> directly from those.  You might be also be able to extract code for
> the '{:f}' formatting from the Python 2.6 Decimal source (Lib/
> decimal.py), but it's fairly convoluted.

Thanks a lot for the hints, I'll look into that.

Holger

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01



More information about the Python-list mailing list