gpt4 book ai didi

python - 使用 Python 舍入到最接近的 0.05

转载 作者:太空狗 更新时间:2023-10-29 17:21:24 25 4
gpt4 key购买 nike

如何在 python 中进行以下舍入:

四舍五入到最接近的小数点 0.05

7,97 -> 7,95

6,72 -> 6,70

31,06 -> 31,05

36,04 -> 36,05

5,25 -> 5,25

希望它有意义。

最佳答案

def round_to(n, precision):
correction = 0.5 if n >= 0 else -0.5
return int( n/precision+correction ) * precision

def round_to_05(n):
return round_to(n, 0.05)

关于python - 使用 Python 舍入到最接近的 0.05,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4265546/

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