例子
下面的例子显示了round()方法的使用
#!/usr/bin/python2 print "round(80.23456, 2) : ", round(80.23456, 2) print "round(100.000056, 3) : ", round(100.000056, 3) print "round(-100.000056, 3) : ", round(-100.000056, 3)
当我们运行上面的程序,它会产生以下结果:
round(80.23456, 2) : 80.23 round(100.000056, 3) : 100.0 round(-100.000056, 3) : -100.0
代码:
#!/usr/bin/python2 print round(2.635, 2) print round(2.645, 2) print round(2.655, 2) print round(2.665, 2) print round(2.675, 2)
输出结果:
2.63 2.65 2.65 2.67 2.67
如果大家不需要四舍五入的话,也可以考虑使用我们最熟悉的print("%.2f" % 2.675)这种方式实现。
查看更多关于round四舍五入详解--python2与python3版本间区别的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did86948