Reputation: 2263
I want to print scientific notation number with same scale in python. For example I have different numbers (1e-6, 3e-7, 5e-7, 4e-8)
so now I want to print all these numbers with e-6
notation (1e-6, 0.3e-6, 0.5e-6, 0.04e-6)
. So how can I print it?
Thanks
Upvotes: 4
Views: 960
Reputation: 20329
As you're using numpy, you should definitely take a look at the np.set_printoptions
function, in particular its formatter
argument that takes a dictionary like {'float': formatting_function}
. Use the most appropriate formatting function for your particular case.
Upvotes: 0
Reputation: 250871
using log10
:
In [64]: lis= (1e-6, 3e-7, 5e-7, 4e-8)
In [65]: import math
In [66]: for x in lis:
exp=math.floor(math.log10(x))
num=x/10**exp
print "{0}e-6".format(num*10**(exp+6))
....:
....:
1.0e-6
0.3e-6
0.5e-6
0.04e-6
Upvotes: 1
Reputation: 365587
Mathematically, tacking on e-6
is the same as multiplying by 1e-6
, right?
So, you've got a number x
, and you want to know what y
to use such that y * 1e-6 == x
.
So, just multiply by 1e6, render that in non-exponential format, then tack on e-6
as a string:
def e6(x):
return '%fe-6' % (1e6 * x,)
Upvotes: 3