扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python中round函数如何使用

扬州沐宇科技
2024-04-19 10:29:03
python

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

语法:

round(number, ndigits)

参数:

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

示例:

result1 = round(3.14159)
print(result1)  # 输出:3

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

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

扫码添加客服微信