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. android-webview - Android 7.0 webview不自动刷新2. vue.js - vue开发的项目,想在页面打开的时候使input自动聚焦,使用的官方的自定义指令,但是在ios真机上测试无效3. javascript - 用atom安装linter-eslint插件出错4. shell - mac bash: mysql: command not found5. javascript - 怎么在DIV图片上加个DIV,并带有按钮链接呢?6. python - 用ffmpeg采集视频,用terminate没法结束进程7. javascript - 用console.log调试JS是不是很不专业?8. ruby - 添加国内淘宝gem源失败9. javascript - 关于pc端网站的浏览器兼容问题!10. MySQL的合计函数这里为什么报错?

网公网安备