简体   繁体   English

以科学记数法python优雅地打印

[英]Print elegantly in scientific notation python

I wanna print a number and its uncertainty in the following way:我想通过以下方式打印一个数字及其不确定性:

x = (4.23 ± 0.01) e3 x = (4.23 ± 0.01) e3

where the value and its uncertainty are two different variables.其中价值及其不确定性是两个不同的变量。

mean = 4230
uncertainty = 10 
print("x is %.2e \u00B1 %.2e" % (mean, uncertainty))

I want them to be formatted with the same power of the exponential in the scientific notation.我希望它们的格式与科学记数法中指数的幂相同。 what I get is我得到的是

x is 4.23e+03 ± 1.00e+01

Assuming mean integer, a way to do it might be假设平均整数,一种方法可能是

>>> mean = 4230
>>> uncertainty = 10 
>>> pos_to_first = len(str(mean)) - 1
>>> f"({mean/10**pos_to_first} ± {uncertainty/10**pos_to_first}) E{pos_to_first}"
'(4.23 ± 0.01) E3'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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