Python - Round to nearest 05

后端 未结 9 1930
死守一世寂寞
死守一世寂寞 2020-11-29 08:14

Hvor can I en python do the following rounding:

Round to the nearest 05 decimal

7,97 -> 7,95

6,72 -> 6,70

31,06 -> 31,05

36,04 -> 36,

9条回答
  •  醉梦人生
    2020-11-29 09:15

    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)
    

提交回复
热议问题