首页 / 知识

python如何使用round函数

2023-11-12 13:11:00

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

$python

Python2.7.8(default,Jun182015,18:54:19)

[GCC4.9.1]onlinux2

Type"help","copyright","credits"or"license"formoreinformation.

>>>round(0.5)

1.0

$python3

Python3.4.3(default,Oct142015,20:28:29)

[GCC4.8.4]onlinux

Type"help","copyright","credits"or"license"formoreinformation.

>>>round(0.5)

0

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

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

如果digits大于0,则四舍五入到指定的小数位。

如果digits等于0,则四舍五入到最接近的整数。

如果digits小于0,则在小数点左侧进行四舍五入。

如果round函数只有参数number,等同于digits等于0。

以上内容为大家介绍了python培训之如何使用round函数,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注我们

培训函数小数位小数点整数

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢