471,089 Members | 1,336 Online

# unusual exponential formatting puzzle

Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

That is, the leading digit is always 0, instead of the first significant
digit. It is fixed width. I can almost get it with '% 16.9E', but not
quite.

My solution is to print to a string with the '% 16.9E' format, then parse it
with re to pick off the pieces and fix it up. Pretty ugly. Any better
ideas?
Sep 21 '05 #1
5 1940

Neal Becker wrote:
Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

That is, the leading digit is always 0, instead of the first significant
digit. It is fixed width. I can almost get it with '% 16.9E', but not
quite.

My solution is to print to a string with the '% 16.9E' format, then parse it
with re to pick off the pieces and fix it up. Pretty ugly. Any better
ideas?

If you have gmpy available...
import gmpy
s = gmpy.pi(64)
s mpf('3.14159265358979323846e0',64)

....you can use the fdigits function
t = gmpy.fdigits(s,10,8,0,0,2)
....to create a seperate digit string and exponent...
print t ('31415927', 1, 64)

....which can then be printed in the desired format.
print "0.%sE%02d" % (t[0],t[1])

0.31415927E01

Sep 21 '05 #2

me********@aol.com wrote:
Neal Becker wrote:
Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

That is, the leading digit is always 0, instead of the first significant
digit. It is fixed width. I can almost get it with '% 16.9E', but not
quite.

My solution is to print to a string with the '% 16.9E' format, then parse it
with re to pick off the pieces and fix it up. Pretty ugly. Any better
ideas?

If you have gmpy available...
import gmpy
s = gmpy.pi(64)
s mpf('3.14159265358979323846e0',64)

...you can use the fdigits function
t = gmpy.fdigits(s,10,8,0,0,2)
...to create a seperate digit string and exponent...
print t ('31415927', 1, 64)

...which can then be printed in the desired format.
print "0.%sE%02d" % (t[0],t[1]) 0.31415927E01

print "0.%sE%02d" % (t[0],t[1]) 0.-31415927E03

Drat. Needs work.

And does the format permit large negative exponents (2 digits + sign)?
print "0.%sE%02d" % (t[0],t[1])

0.31415927E-13

Sep 21 '05 #3
me********@aol.com wrote:

me********@aol.com wrote:
Neal Becker wrote:
> Like a puzzle? I need to interface python output to some strange old
> program. It wants to see numbers formatted as:
>
> e.g.: 0.23456789E01
>
> That is, the leading digit is always 0, instead of the first
> significant
> digit. It is fixed width. I can almost get it with '% 16.9E', but not
> quite.
>
> My solution is to print to a string with the '% 16.9E' format, then
> parse it
> with re to pick off the pieces and fix it up. Pretty ugly. Any better
> ideas?

If you have gmpy available...
>>> import gmpy

>>> s = gmpy.pi(64)
>>> s

mpf('3.14159265358979323846e0',64)

...you can use the fdigits function
>>> t = gmpy.fdigits(s,10,8,0,0,2)

...to create a seperate digit string and exponent...
>>> print t

('31415927', 1, 64)

...which can then be printed in the desired format.
>>> print "0.%sE%02d" % (t[0],t[1])

0.31415927E01

print "0.%sE%02d" % (t[0],t[1]) 0.-31415927E03

Drat. Needs work.

And does the format permit large negative exponents (2 digits + sign)?

I think the abs (exponent) < 10 for now
print "0.%sE%02d" % (t[0],t[1])

0.31415927E-13

Sep 21 '05 #4
Neal Becker <nd*******@gmail.com> writes:
Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01
Yeah, that was normal with FORTRAN.
My solution is to print to a string with the '% 16.9E' format, then
parse it with re to pick off the pieces and fix it up. Pretty ugly.
Any better ideas?

That's probably the simplest.
Sep 22 '05 #5
Paul Rubin wrote:
Neal Becker <nd*******@gmail.com> writes:
Like a puzzle? I need to interface python output to some strange old
program. It wants to see numbers formatted as:

e.g.: 0.23456789E01

Yeah, that was normal with FORTRAN.
My solution is to print to a string with the '% 16.9E' format, then
parse it with re to pick off the pieces and fix it up. Pretty ugly.
Any better ideas?

That's probably the simplest.

Acutally, I found a good solution using the new decimal module:
def Format(x):
"""Produce strange exponential format with leading 0"""
s = '%.9E' % x

d = decimal.Decimal (s)
(sign, digits, exp) = d.as_tuple()
s = ''
if (sign == 0):
s += ' '
else:
s += '-'

s += '0.'

e = len (digits) + exp
for x in digits:
s += str (x)
s += 'E'
s += '%+03d' % e

return s
Sep 22 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.