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. javascript - 请教如何获取百度贴吧新增的两个加密参数2. javascript - 使用百度文本编辑器ueditor不显示样式问题3. css3 - 关于css的问题,除了倒数第二个td 其他td的宽度都是15%;因为我表格字段数量不确定4. javascript - 能不能用js给一个div添加一个持续的hover的效果,就像有另一个鼠标一直放在上边?5. css - 使用blur()滤镜为什么有透明的效果6. docker - MySQL 报错:Access denied for user ’xxx’@’localhost’7. javascript - 给某个类添加一个伪类,这个类有click事件,现在我点击伪类也触发了click事件8. 如何用笔记本上的apache做微信开发的服务器9. python的 itchat微信api文档的 itchat.send如何发信息给指定用户?10. javascript - autocomplete ajax怎么配置,求教
