python round 四舍五入?
问题描述
>>> round(0.5)0>>> round(1.5)#为什么这样2
问题解答
回答1:在不同的Python版本中,round函数的取值会出现不同状况
在Python2.7中,round函数的定义是如果输入数值距离两边一样远,则取偶数的一边
~ python2Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType 'help', 'copyright', 'credits' or 'license' for more information.>>> round(0.5)1.0
在Python3 中,round函数的定义是四舍五入。
Python 3.6.0 (v3.6.0:41df79263a11, Dec 22 2016, 17:23:13) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType 'help', 'copyright', 'credits' or 'license' for more information.>>> round(0.5)0
楼上那位是用了ipython,是基于python2的,所以定义也遵从Python2的。
回答2:round(number, ndigits=None)指要保留的小数位,默认为None
In [5]: round(0.5)Out[5]: 1.0In [6]: round(0.5, 1)Out[6]: 0.5回答3:
试下round(4.5)
相关文章:
1. node.js - nodeJS 写入文件的内容怎么不是按次序的?2. 用CSS3 box-sizing 属性实现两个并排的容器,如果想让容器中间有间隔该如何实现3. docker gitlab 如何git clone?4. debian - docker依赖的aufs-tools源码哪里可以找到啊?5. javascript - 求一款靠谱点的移动端图片查看器插件,老司机速进!6. javascript - angular左右滑动如何切换路由状态,????7. oop - Java中,关于匿名内部类调用外部类方法的疑惑8. javascript - jquery hide()方法无效9. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?10. 这是什么情况???

网公网安备