exponential float formmating

Z

zunbeltz

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
 
M

Marc 'BlackJack' Rintsch

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
 
Z

zunbeltz

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

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'1.300000e+001'

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

attn.steven.kuo

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
 
S

Steven D'Aprano

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.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top