[英]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.