Convert Scientific Notation to Float Convert Scientific Notation to Float python python

Convert Scientific Notation to Float


You are looking at the default str() formatting of floating point numbers, where scientific notation is used for sufficiently small or large numbers.

You don't need to convert this, the value itself is a proper float. If you need to display this in a different format, format it explicitly:

>>> print(0.00001357)1.357e-05>>> print(format(0.00001357, 'f'))0.000014>>> print(format(0.00001357, '.8f'))0.00001357

Here the f format always uses fixed point notation for the value. The default precision is 6 digits; the .8 instructs the f formatter to show 8 digits instead.

In Python 3, the default string format is essentially the same as format(fpvalue, '.16g'); the g format uses either a scientific or fixed point presentation depending on the exponent of the number. Python 2 used '.12g'.


You can use print formatting:

x = 1.357e-05    print('%f' % x)

Edit:

print('%.08f' % x)


There are some approaches:


#1 float(...) + optionally round() or .format()

x = float(1.357e-05)round(x, 6)"{:.8f}".format(x)

#2 with decimal class

import decimaltmp = decimal.Decimal('1.357e-05')print('[0]', tmp)# [0] 0.00001357tmp = decimal.Decimal(1.357e-05)print('[1]', tmp)# [1] 0.0000135700000000000005188384444299032338676624931395053863525390625decimal.getcontext().prec = 6tmp = decimal.getcontext().create_decimal(1.357e-05)print('[2]', tmp)# [2] 0.0000135700

#3 with .rstrip(...)

x = ("%.17f" % n).rstrip('0').rstrip('.')

Note: there are counterparts to %f:
%f shows standard notation
%e shows scientific notation
%g shows default (scientific if 5 or more zeroes)