Output: Number of digits in exponent?

  • Thread starter Jens Bloch Helmers
  • Start date
J

Jens Bloch Helmers

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.
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: 1.000e+50

Thanks for any help!
Jens
 
P

Paul Rubin

Jens Bloch Helmers said:
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.

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. 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:
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:]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top