简体   繁体   中英

How to print scientific notation number into same scale

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

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,)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM