Python - 四舍五入到最接近的 05
问题描述:
Hvor can I en python 做以下四舍五入:
Hvor can I en python do the following rounding:
四舍五入到最接近的小数点 05
Round to the nearest 05 decimal
7,97 -> 7,95
7,97 -> 7,95
6,72 -> 6,70
6,72 -> 6,70
31,06 -> 31,05
31,06 -> 31,05
36,04 -> 36,05
36,04 -> 36,05
5,25 -> 5,25
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)