[英]How to print scientific notation number into same scale
我想在python中打印相同比例的科學記數。 例如,我有不同的數字(1e-6, 3e-7, 5e-7, 4e-8)
(1e-6, 0.3e-6, 0.5e-6, 0.04e-6)
(1e-6, 3e-7, 5e-7, 4e-8)
所以現在我想用e-6
表示法打印所有這些數字(1e-6, 0.3e-6, 0.5e-6, 0.04e-6)
。 那我怎么打印呢?
謝謝
在數學上,在e-6
上加上與乘以1e-6
,對吧?
所以,你有一個數字x
,你想知道y
使用什么, y * 1e-6 == x
。
因此,只需乘以1e6,以非指數格式呈現,然后將e-6
作為字符串添加:
def e6(x):
return '%fe-6' % (1e6 * x,)
使用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
當你使用numpy時,你一定要看看np.set_printoptions
函數,特別是它的formatter
參數,它接受像{'float': formatting_function}
這樣的字典。 為您的特定情況使用最合適的格式化功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.