python保留小数位的方法
- format函数
print('{:.3f} {:.2f}'.format(1.23456,0.1256))#1.235 0.13
:.3f :3f表示保留3位有效数字
print(format(1.2345,'.2f'))#1.23
.2f :表示小数点后保留两位有效数字
使用了占位符{}可以同时输出多个,后者一次只能输出一个,需要注意的是占位符中的冒号不能丢
- ’%.xf‘ 方法
print('%.2f'%1.2356)#1.24
- 通过round()函数
print(round(1.2345,3))#1.234
print(round(2.3568,2))#2.36
#大于等于六时进位
print(round(2.9))#3
print(round(2.6))#3
print(round(2.5))#2
print(round(2.1))#2