我希望将一些小数字转换为简单、可读的输出。这是我的方法,但我想知道是否有更简单的方法。
x = 8.54768039530728989343156856E-58
y = str(x)
print "{0}.e{1}".format(y.split(".")[0], y.split("e")[1])
8.e-58
最佳答案
这让你很接近,你需要 8.e-58
确切地说,还是您只是想将其缩短为可读的内容?
>>> x = 8.54768039530728989343156856E-58
>>> print "{0:.1e}".format(x)
8.5e-58
替代方案:
>>> print "{0:.0e}".format(x)
9e-58
请注意,在 Python 2.7 或 3.1+ 上,您可以省略表示位置的第一个零,因此它类似于 "{:.1e}".format(x)
。
关于python - "rounding"python中的负指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10644753/