Python保留指定位数的小数

方法:

  1. ’%.2f’ %f 方法(推荐)
f = 1.23456

print('%.4f' % f)
print('%.3f' % f)
print('%.2f' % f)

  1. format函数
print(format(1.23456, '.2f'))
print(format(1.23456, '.3f'))
print(format(1.23456, '.4f'))

  1. round函数(四舍五入)
a = 1.23456
b = 2.355
c = 3.5
d = 2.5
print(round(a, 3))
print(round(b, 2))
print(round(c))
print(round(d))

参考链接:
https://blog.csdn.net/liuweiyuxiang/article/details/100574386

你可能感兴趣的:(python)