python的round函数使用

 

碰到的问题: 对float进行精确两位显示出来。 

解决的方法:round(3.32342,2)  #3.32  .

round函数概念:

英文:圆,四舍五入
是python内置函数,它在哪都能用,对数字取四舍五入。
round(number[, ndigits])
round 对传入的数据进行四舍五入,如果ngigits不传,默认是0(就是说保留整数部分).ngigits<0 的时候是来对整数部分进行四舍五入,返回的结果是浮点数.

round 负数

四舍五入是围绕着0来计算的,
round(0.5)            # 1.0
round(-0.5)         #-1.0

round 的陷阱

round(1.675, 2)    #1.68
round(2.675, 2)    #2.67

举例:

round(3.4)         # 3.0
round(3.5)         # 4.0
round(3.6)         # 4.0
round(3.6, 0)      # 4.0
round(1.95583, 2)  # 1.96
round(1241757, -3) # 1242000.0
round(5.045, 2)    # 5.05
round(5.055, 2)    # 5.06


参考文档:http://docs.python.org/2.7/library/functions.html#round

posted on 2012-11-09 20:43  DON&#39;T PANIC  阅读(38720)  评论(0编辑  收藏  举报

导航