Python round() 函数

介绍

Python 中的 round() 函数用于将小数值四舍五入为最接近的 10 的倍数。您还可以指定函数四舍五入的小数位数。 如果数字小于5,则保留其旁边的数字,如果大于5,则将其旁边的数字向上取整为1个单位。

现在我们将指导您在 Python 中使用 round() 函数。 希望你能理解。

例子

x = round(3.475, 2)

print(x)

输出:

3.48

定义

round() 函数四舍五入到您指定的小数位。

如果数字小于5,则保留其旁边的数字,如果大于5,则将其旁边的数字向上取整为1个单位。

语法

round(number, digits)

参数值:

number:要四舍五入的小数位数(必填)

digits:要舍入到的小数位。 默认为 0(可选)

更多例子

示例 1: 基本的 round() 函数

print(round(13.4))

print(round(12.6))

print(round(10.3))

输出:

13

13

10

示例 2: 指定舍入位置

x = round(13.4567, 2)

y = round(12.23456, 3)

print(x)

print(y)

输出:
13.46

12.235

示例 3: Python round() 向上

print(round(11.6))

print(round(14.7))

输出:

12

15

例 4: Python round() 向下

print(round(11.3))

print(round(14.5))

输出:

11

14

结论

我们还指导您如何在 Python 中使用 round() 函数。

感谢您查看!