问答网

当前位置: 首页 > 知识问答 > round函数怎么使用的

round函数怎么使用的

知识问答 浏览4次

使用方法如下:

1. round(x):对x进行四舍五入,默认保留到整数。

2. round(x, n):对x进行四舍五入,保留n位小数。n可以为负数,表示保留到整数位。

例如:

```python

x = 3.1415926

print(round(x)) # 输出:3

y = 3.1415926

print(round(y, 2)) # 输出:3.14

z = 3.1415926

print(round(z, -1)) # 输出:0

```

在这些例子中,`round(x)`将3.1415926四舍五入到最近的整数3,`round(y, 2)`将3.1415926四舍五入到两位小数3.14,`round(z, -1)`将3.1415926四舍五入到最近的十位数0。