Python-舍入到最接近的05

发布于 2021-01-29 19:09:48

我可以在python中进行以下舍入:

四舍五入到最接近的05小数

7,97-> 7,95

6,72-> 6,70

31,06-> 31,05

36,04-> 36,05

5,25-> 5,25

希望有道理。

关注者
0
被浏览
52
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。
    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)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看