format函数保留指定位数的小数:
print(format(1.23456, '.2f')) print(format(1.23456, '.3f')) print(format(1.23456, '.4f'))
输出
1.2346 1.235 1.23
a = 3.75 print(round(a)) >>> 4 b = 3.25 print(round(b)) >>> 3 c = 2.85 print(round(c,1)) # 保留一位小数 >>> 2.9
上一篇:怎么实现python中super()多重继承
下一篇:round在python取舍时需要注意哪些事项