python中round函数如何使用

   2024-10-20 2920
核心提示:round函数在Python中用来对一个数字进行四舍五入。语法:round(number, ndigits)参数:number:要四舍五入的数字ndigits:保留的

round函数在Python中用来对一个数字进行四舍五入。

语法:

round(number, ndigits)

参数:

number:要四舍五入的数字ndigits:保留的小数位数,默认为0(表示对整数四舍五入)

示例:

result1 = round(3.14159)print(result1)  # 输出:3result2 = round(3.14159, 2)print(result2)  # 输出:3.14

在上面的示例中,第一个round函数将3.14159四舍五入为3,而第二个round函数将3.14159四舍五入为2位小数的3.14。

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号