python3-round函数使用
2020-12-24 本文已影响0人
千年积木
#round
#四舍五入到最近的整数
print(round(100.234)) #100
print(round(100.234,0)) #100.0
#保留指定位数小数
print(round(100.234,2)) #100.23
print(round(100.234,3)) #100.234
#对整数位数进行四舍五入
print(round(100.234,-1)) #100.0
print(round(100.234,-4)) #0.0
print(round(10011.234,-2)) #10000.0
print(round(100123.234,-3)) #100000.0
print(round(146123.234,-3)) #146000.0
#到两端的距离一样,则往偶数端取值
print(round(0.5)) #0
print(round(-0.5)) #0
#转换成二进制1,0 后可能无限位数,截断后可能比2.35小一点点,四舍五入后就是2.35
print(round(2.355,2)) #2.35
print(round(2.755,2)) #2.75