文章来源地址:http://www.cnblogs.com/anpengapple/p/6507271.html
这个一直都想写,但是因为这个点比较小,所以一直懒得动手。不过还是补上吧,留着早晚是个祸害。
round函数很简单,对浮点数进行近似取值,保留几位小数。比如:>>> round(10.0/3, 2)3.33>>> round(20/7)3
第一个参数是一个浮点数,第二个参数是保留的小数位数,可选,如果不写的话默认保留到整数。
这么简单的函数,能有什么坑呢?
1、round的结果跟python版本有关
我们来看看python2和python3中有什么不同:$ pythonPython 2.7.8 (default, Jun 18 2015, 18:54:19) [GCC 4.9.1] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> round(0.5)1.0
$ python3Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linuxType "help", "copyright", "credits" or "license" for more information.>>> round(0.5)0
好玩吗?
如果我们阅读一下python的文档,里面是这么写的:
在python2.7的doc中,round(