Soviero
Soviero

Reputation: 1902

Converting scientific notation to human readable float

How can I programmatically take a float like this:

1.87491348956e-28

and convert it into a float (likely a string) like this:

0.000000000000000000000000000187491348956

(I think that's the right number of zeros)

Upvotes: 1

Views: 2061

Answers (3)

Christian K.
Christian K.

Reputation: 2823

a=1.87491348956e-28
base,expo = str(a).split('e')
print '%%1.%df'%(len(base)-2+abs(int(expo)))%a

Edit: I forgot to remove some formatting parts. The latest version is a little shorter.

How does it work?

You could call the format string a recursive one. In a first step only the inner part, i.e. %d is interpreted as format information and replaced by the calculated length of the floating point number. Let's call that length X. Thus, after the first formatting, the remaining format string reads %1.Xf and is used to actually format the number. Note that the %% got replaced by a single % after the first step.

Edit:

Rob_sigma added a comment with an easier solution:

a=1.87491348956e-28
base,expo = str(a).split('e')
print '%.*f'%(len(base)-2+abs(int(expo)),a)

I must admit that I have never seen that syntax before.

Upvotes: 5

Soviero
Soviero

Reputation: 1902

Based on the other answers, I was able to create what I wanted:

f = 1.23456789e-28
d = abs(int(str(f).split('e')[1]))
print format(f, '.{0}f'.format(d)) + ' %'

The difference is that the function above will automatically make a human-readable float that is always exactly as many digits as are necessary. Also, I can do a "d += 10" or similar to add additional significant digits.

Here's some example output to see what I mean:

0.0000000000000000000000000000003 %
0.0000000000000000000000000000006 %
0.0000000000000000000000000000009 %
0.000000000000000000000000000001 %
0.000000000000000000000000000001 %
0.000000000000000000000000000002 %
0.000000000000000000000000000002 %
0.000000000000000000000000000002 %
0.000000000000000000000000000003 %
0.000000000000000000000000000003 %
0.000000000000000000000000000003 %
0.000000000000000000000000000004 %
0.000000000000000000000000000004 %
0.000000000000000000000000000004 %
0.000000000000000000000000000004 %
0.000000000000000000000000000005 %
0.000000000000000000000000000005 %
0.000000000000000000000000000005 %
0.000000000000000000000000000006 %
0.000000000000000000000000000006 %
0.000000000000000000000000000006 %
0.000000000000000000000000000006 %
0.000000000000000000000000000007 %
0.000000000000000000000000000007 %
0.000000000000000000000000000007 %
0.000000000000000000000000000008 %
0.000000000000000000000000000008 %
0.000000000000000000000000000008 %
0.000000000000000000000000000009 %
0.000000000000000000000000000009 %
0.000000000000000000000000000009 %
0.000000000000000000000000000009 %
0.000000000000000000000000000010 %
0.000000000000000000000000000010 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000001 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %
0.00000000000000000000000000002 %

See how as the number gets larger, the resulting float gets shorter to match.

Upvotes: 0

gkusner
gkusner

Reputation: 1244

count the number of digits after the decimal you have and add the + exponent to get the output precision:

'%.39f' % 1.87491348956e-28

Upvotes: 1

Related Questions