python培训之如何使用round函数

发布时间:2022-09-16 09:46:08 人气:106 作者:多测师

  在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培训之如何使用round函数

  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培训之如何使用round函数,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线