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. $fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题2. MYSQL新建用户设置可以远程访问的问题3. java - mybatis怎么实现在数据库中有就修改,没有就添加4. python - 求一个在def中可以实现调用本def满足特定条件continue效果的方法(标题说不太清楚,请见题内描述)5. angular.js - angularjs的自定义过滤器如何给文字加颜色?6. javascript - 用表单提交两个时间段请求后台返回对应数据时出现的一些问题!7. node.js - nodejs和前端JavaScript 字符串处理结果不一样是什么原因?8. linux - 为什么我在mysql的my.cnf下找不到bind-address?9. mysql - SQL操作时间的函数?10. 各位谁知道这个CSS时候哪里出错了???
![$fields = $values = [];这条代码一直定义不了,一直报错,老师的源码也是被报错的,执行不了,请问该怎么解决这个问题](http://www.haobala.com/attached/image/news/202205/093622cb60.png)