gpt4 book ai didi

python - "rounding"python中的负指数

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:49 25 4
gpt4 key购买 nike

我希望将一些小数字转换为简单、可读的输出。这是我的方法,但我想知道是否有更简单的方法。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com