品易云推流 关闭
文章详情页
文章 > Python常见问题 > python中如何使用round函数

python中如何使用round函数

round

头像

silencement

2019-11-12 14:51:3429198浏览 · 0收藏 · 0评论

在python3中,round(1.0/2.0)得到的是1,而在python2中,round(1.0/2.0)得到的是0

$ python
Python 2.7.8 (default, Jun 18 2015, 18:54:19) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
1.0
$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.5)
0

使用的一般语法是round(number,digits)

number,要四舍五入的数,digits是要小数点后保留的位数

如果 digits 大于 0,则四舍五入到指定的小数位。 
如果 digits 等于 0,则四舍五入到最接近的整数。 
如果 digits 小于 0,则在小数点左侧进行四舍五入。
如果round函数只有参数number,等同于digits 等于 0。

返回的值是四舍五入后的值。python学习网,大量的免费python学习视频,欢迎在线学习!

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图