python 中 round函数实现保留特定位数、四舍五入

 

001、

>>> a = [1, 3, 6]
>>> sum(a)/len(a)
3.3333333333333335
>>> round(sum(a)/len(a), 2)     ## 保留两位有效数字
3.33
>>> round(sum(a)/len(a), 4)     ## 保留四位有效数字
3.3333
>>> 2/3
0.6666666666666666
>>> round(2/3, 2)               ## 保留两位有效数字,四舍五入
0.67
>>> round(2/3, 3)               ## 保留三位有效数字,四舍五入
0.667

 

posted @ 2022-08-08 13:47  小鲨鱼2018  阅读(306)  评论(0编辑  收藏  举报