Python - 四舍五入到最接近的 05 [英] Python - Round to nearest 05

查看:61
本文介绍了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)

这篇关于Python - 四舍五入到最接近的 05的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆