python3对末尾数为0保留N位小数四舍五入(例如:0.150)

0.150,保留2位小数四舍五入

def Rounding(_float, _len):
    try:
        if str(_float).split(".")[1][_len] == '5':
            return (round(float(str(_float)[:-1] + '6'), _len))
        else:
            return (round(_float, _len))
    except:
        return((_float))


a=0.150

print(Rounding(a,2))


0.15
发布了58 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42846555/article/details/102481203