exponential float formmating

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Sep 7 23:28:00 EDT 2007


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.



More information about the Python-list mailing list