Output: Number of digits in exponent?

Jens Bloch Helmers Jens.DOTBloch.DOTHelmers at ATdnv.com
Tue Dec 6 06:29:51 EST 2005


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
>>> print '%e' % (1.0e50)
1e+050

I would prefer two digits in the exponent. Usually 3 digits is not a
problem, except for waisted disk space in 99.999999% of all practical
cases.

But this time I have to write an interface file to a program written
in an other programming language, and the output format requires a fix
fortran format with 2 digits in the exponent.

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

Thanks for any help!
Jens



More information about the Python-list mailing list