如何将 Python 变量(浮点型)限制为 5 位数?
How can I limit to 5 digits a Python variable (float type)?
我有变量
current_price = 1.158805
但我需要限制为 5 个数字(在这种情况下我想要
当前价格为 1.15880),我该怎么办?
我需要一个通用的解决方案,因为变量每秒都在变化。
如果我在 pandas 数据框中有数字怎么办,例如 data.close.iloc[-1]
?
current_price = 1.158805
print(str(current_price)[:-1])
您的输出将是:
>>> 1.15880
我有变量
current_price = 1.158805
但我需要限制为 5 个数字(在这种情况下我想要 当前价格为 1.15880),我该怎么办?
我需要一个通用的解决方案,因为变量每秒都在变化。
如果我在 pandas 数据框中有数字怎么办,例如 data.close.iloc[-1]
?
current_price = 1.158805
print(str(current_price)[:-1])
您的输出将是:
>>> 1.15880