今天用python3中的round想要得到四舍五入的数,结果发现,round(2.5)打印结果为2,round3.5打印结果为4.于是就怀疑人生了.
下面是我在菜鸟教程找到的说明.
如果我们阅读一下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。
来源为菜鸟教程:地址为:http://www.runoob.com/w3cnote/python-round-func-note.html