Python Floating Point Formatting
I've seen a few questions about this already, but none that I read helped me actually understand why what I am trying to do is failing. So I have a bunch of floating point values,
Solution 1:
>>> '{:.7e}'.format(0.00000000000000365913456789)
'3.6591346e-15'
Solution 2:
You can use the scientific notation format: Something like this:
number = '%e' % oldnumber
>>> x = 1.759374
>>> print '%e' % x
1.759374e+00
>>>
>>> x = 1.79
>>> print '%e' % x
1.790000e+00
>>>
>>> x = 1.798775655
>>> print '%e' % x
1.798776e+00
>>>
Or, if you want to control precision, you can use the format method as sugged by @leon approach (+1).
>>> x = 1.759374
>>>
>>> print('{:.2e}'.format(x))
1.76e+00
>>>
>>> print('{:.10e}'.format(x))
1.7593740000e+00
>>>
>>> print('{:.4e}'.format(x))
1.7594e+00
>>>
Post a Comment for "Python Floating Point Formatting"