python中关于round函数的⼩坑
这个⼀直都想写,但是因为这个点⽐较⼩,所以⼀直懒得动⼿。不过还是补上吧,留着早晚是个祸害。
round函数很简单,对浮点数进⾏近似取值,保留⼏位⼩数。⽐如
>>> round(10.0/3, 2)
3.33
>>> round(20/7)
3
第⼀个参数是⼀个浮点数,第⼆个参数是保留的⼩数位数,可选,如果不写的话默认保留到整数。
这么简单的函数,能有什么坑呢?
1、round的结果跟python版本有关
我们来看看python2和python3中有什么不同:
$ python
Python 2.7.8 (default, Jun 18 2015, 18:54:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits"or"license"for more information.
>>> round(0.5)
1.0
$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits"or"license"for more information.
>>> round(0.5)round函数有几个参数
好玩吗?
如果我们阅读⼀下python的⽂档,⾥⾯是这么写的:
在python2.7的doc中,round()的最后写着,“Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done
away from 0.” 保留值将保留到离上⼀位更近的⼀端(四舍六⼊),如果距离两端⼀样远,则保留到离0远的⼀边。所以round(0.5)会近似到1,⽽round(-0.5)会近似到-1。
但是到了python3.5的doc中,⽂档变成了“values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward
the even choice.” 如果距离两边⼀样远,会保留到偶数的⼀边。⽐如round(0.5)和round(-0.5)都会保留到0,⽽round(1.5)会保留到2。
所以如果有项⽬是从py2迁移到py3的,可要注意⼀下round的地⽅(当然,还要注意/和//,还有print,还有⼀些⽐较另类的库)。
2、特殊数字round出来的结果可能未必是想要的。
>>> round(2.675, 2)
2.67
python2和python3的doc中都举了个相同的栗⼦,原⽂是这么说的:
Note
The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating P
简单的说就是,round(2.675, 2) 的结果,不论我们从python2还是3来看,结果都应该是2.68的,结果它偏偏是2.67,为什么?这跟浮点数的精度有关。我们知道在机器中浮点数
不⼀定能精确表达,因为换算成⼀串1和0后可能是⽆限位数的,机器已经做出了截断处理。那么在机器中保存的2.675这个数字就⽐实际数字要⼩那么⼀点点。这⼀点点就导致
了它离2.67要更近⼀点点,所以保留两位⼩数时就近似到了2.67。
以上。除⾮对精确度没什么要求,否则尽量避开⽤round()函数。近似计算我们还有其他的选择:
1. 使⽤math模块中的⼀些函数,⽐如iling(天花板除法)。
2. python⾃带整除,python2中是/,3中是//,还有div函数。
3. 字符串格式化可以做截断使⽤,例如 "%.2f" % value(保留两位⼩数并变成字符串……如果还想⽤浮点数请披上float()的外⾐)。
4. 当然,对浮点数精度要求如果很⾼的话,请⽤嘚瑟馍,不对不对,请⽤decimal模块。
就酱。