gpt4 book ai didi

python 2.7+ : What's the correct way to format a float in non-scientific notation with certain precision?

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

我希望我能理解这个问题。

有时,如果我打印一个小 float ,它看起来像 6.1248979238e-05 或类似的东西。

我希望能够说“无论如何,像这样输出 10 位精度”:0.abcdefghij

最佳答案

>>> '%0.10f'% 6.1248979238e-05 
'0.0000612490'

这是“字符串插值”或printf-style formatting仍然得到广泛支持和使用。另一个选项是 newer style string formatting :

>>> '{:.10f}'.format(6.1248979238e-05)
'0.0000612490'

或者如果你想兼容python2.6:

>>> '{0:.10f}'.format(6.1248979238e-05)
'0.0000612490'

随你便。您使用哪个版本取决于您要支持的 python 版本。如果您 python2.7,那没关系。如果您想支持早期版本,请使用 printf 样式。在我的大部分代码中,我个人已经改用最后一种形式。我不关心 python2.5 支持,但 python2.6 仍然足够新,我希望尽可能支持它。

关于 python 2.7+ : What's the correct way to format a float in non-scientific notation with certain precision?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14143211/

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