温馨提示×

python中round函数如何使用

小亿
116
2024-04-19 10:29:03
栏目: 编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:sql round()函数怎么使用

0