gpt4 book ai didi

python - 打印带有特定数量的零的 float

转载 作者:太空狗 更新时间:2023-10-29 22:06:09 25 4
gpt4 key购买 nike

我知道如何控制小数位数,但我该如何具体控制零的个数?

例如:

104.06250000 -> 104.0625   
119.00000 -> 119.0
72.000000 -> 72.0

最佳答案

使用 decimal 模块怎么样?

来自documentation :

“小数模块包含有效位的概念,因此 1.30 + 1.20 等于 2.50。保留尾随零以表示重要性。这是货币应用程序的习惯表示法。对于乘法,“教科书”方法使用所有被乘数中的数字。例如,1.3 * 1.2 给出 1.56,而 1.30 * 1.20 给出 1.5600。

normalize()函数删除尾随零:

>>> from decimal import *
>>> d1 = Decimal("1.30")
>>> d2 = Decimal("1.20")
>>> d3
Decimal("1.5600")
>>> d3.normalize()
Decimal("1.56")

关于python - 打印带有特定数量的零的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/981189/

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