Output: Number of digits in exponent?

Paul Rubin http
Tue Dec 6 22:33:23 EST 2005


Jens Bloch Helmers <Jens.DOTBloch.DOTHelmers at ATdnv.com> writes:
> How can I control the number of digits in the exponent when writing
> floats to a file? It seems that Python2.4.2(winXP) prints three
> digits anyway.
> 
> >>> print 1.0e50
> 1e+050

That's weird; must be version and/or OS dependent.  On Fedora Core 4:

    Python 2.4.1 (#1, May 16 2005, 15:19:29)
    [GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print 1e50
    1e+50

> Can this be done in Python? Speed is an issue so I don't like the
> idea of rolling my own output function.
> 
> It should be possible to do:
> >>> print '%12.5.2e' % (1.0e50)
>    1.000e+50

How's this (untested):

   def efmt(x):
     s = '%12.5e' % x
     assert s[-5] == 'e' and s[-3] == '0'
     return s[:-4] + s[-2:]



More information about the Python-list mailing list