467,907 Members | 1,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,907 developers. It's quick & easy.

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
  • viewed: 1870
Share:
5 Replies

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
....and your floats are mpf's...
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
...and your floats are mpf's...
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


Unless your numbers are negative.
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


...and your floats are mpf's...
>>> 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


Unless your numbers are negative.
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.

Similar topics

1 post views Thread by Mahesha | last post: by
reply views Thread by Darin McGrew | last post: by
5 posts views Thread by zunbeltz | last post: by
4 posts views Thread by honey777 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.