471,344 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

exponential float formmating

Hi,

For compatibility reasons with an old program I have to format string
in exponential
format with the following format

0.xxxxxE-yy

This means that the number start always by 0 and after the exponent
there should be alway the sing and 2 number for the exponent.

for example 13 shoud be 0.13000E+02
I always get 1.30000E001

Thanks,

Zunbeltz

Sep 7 '07 #1
5 6986
On Fri, 07 Sep 2007 06:08:19 -0700, zunbeltz wrote:
For compatibility reasons with an old program I have to format string
in exponential format with the following format

0.xxxxxE-yy

This means that the number start always by 0 and after the exponent
there should be alway the sing and 2 number for the exponent.

for example 13 shoud be 0.13000E+02
I always get 1.30000E001
I don't know if this is platform dependent but this works for me:

In [41]: '%e' % 1.3
Out[41]: '1.300000e+00'

In [42]: ('%e' % 1.3).upper()
Out[42]: '1.300000E+00'

Ciao,
Marc 'BlackJack' Rintsch
Sep 7 '07 #2
On 7 sep, 15:42, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Fri, 07 Sep 2007 06:08:19 -0700, zunbeltz wrote:
For compatibility reasons with an old program I have to format string
in exponential format with the following format
0.xxxxxE-yy
This means that the number start always by 0 and after the exponent
there should be alway the sing and 2 number for the exponent.
for example 13 shoud be 0.13000E+02
I always get 1.30000E001

I don't know if this is platform dependent but this works for me:

In [41]: '%e' % 1.3
Out[41]: '1.300000e+00'

In [42]: ('%e' % 1.3).upper()
Out[42]: '1.300000E+00'

Ciao,
Marc 'BlackJack' Rintsch
I am working in windows I get
>>'%e' % 13
'1.300000e+001'

In all cases I need the number to start with 0
0.13000E+01

Sep 7 '07 #3
On Sep 7, 6:08 am, zunbe...@gmail.com wrote:
Hi,

For compatibility reasons with an old program I have to format string
in exponential
format with the following format

0.xxxxxE-yy

This means that the number start always by 0 and after the exponent
there should be alway the sing and 2 number for the exponent.

for example 13 shoud be 0.13000E+02
I always get 1.30000E001

Perhaps it would then be worthwhile to subclass float?

import math
class ffloat(float):
"""Formatted float?"""
def __str__(self):
prefix = (self < 0) and '-' or ''
fabs = math.fabs(self)
exponent = math.floor(math.log10(fabs)) + 1
significand = fabs / math.pow(10, exponent)
width = exponent 0 and 2 or 3
return '%s%fE%0*d' % (prefix,significand,width,exponent)
f = ffloat(13)
print f
f = ffloat(-12.23e-4)
print f
--
Hope this helps,
Steven

Sep 7 '07 #4
On Fri, 07 Sep 2007 13:42:30 +0000, Marc 'BlackJack' Rintsch wrote:
On Fri, 07 Sep 2007 06:08:19 -0700, zunbeltz wrote:
>For compatibility reasons with an old program I have to format string
in exponential format with the following format

0.xxxxxE-yy

This means that the number start always by 0 and after the exponent
there should be alway the sing and 2 number for the exponent.

for example 13 shoud be 0.13000E+02
I always get 1.30000E001

I don't know if this is platform dependent but this works for me:

In [41]: '%e' % 1.3
Out[41]: '1.300000e+00'

In [42]: ('%e' % 1.3).upper()
Out[42]: '1.300000E+00'
Alas, you've missed the Original Poster's requirement that the formatted
number always starts with 0.

Also, an easier way to format floats with a capital E is to use %E
instead of %e.

I don't believe there is a standard way of formatting floats with a
leading zero.
--
Steven.
Sep 8 '07 #5
In message <11**********************@50g2000hsm.googlegroups. com>,
zu******@gmail.com wrote:
In all cases I need the number to start with 0
0.13000E+01
So why not move the decimal point left one, and add one to the exponent.
Sep 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Timothy Fitz | last post: by
1 post views Thread by Mahesha | last post: by
1 post views Thread by diegoandrade | last post: by
8 posts views Thread by Martin Jørgensen | last post: by
8 posts views Thread by pereges | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.