• 技术文章 >常见问题 >Python常见问题

    python中如何使用round函数

    silencementsilencement2019-11-12 14:51:34原创28431

    在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学习视频,欢迎在线学习!

    专题推荐:round
    品易云
    上一篇:python是函数式语言吗 下一篇:python如何备注

    相关文章推荐

    • round四舍五入在python2与python3版本间区别• python中round函数怎么用• python如何查询函数• 什么是python内置函数

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网