1. 使用 `int()` 函数:
`int()` 函数会将浮点数向下取整,即截断小数部分。
```python
x = 3.14
y = int(x)
print(y) 输出:3
2. 使用 `math.floor()` 函数:
`math.floor()` 函数会将浮点数向下取整,返回小于或等于原始浮点数的最大整数。
```python
import math
x = 3.14
y = math.floor(x)
print(y) 输出:3
3. 使用 `math.trunc()` 函数:
`math.trunc()` 函数会直接截取浮点数的整数部分,不考虑正负号。
```python
import math
x = 3.14
y = math.trunc(x)
print(y) 输出:3
4. 使用 `round()` 函数:
`round()` 函数默认将浮点数四舍五入到最接近的整数。
```python
x = 3.14
y = round(x)
print(y) 输出:3
5. 使用 `math.ceil()` 函数:
`math.ceil()` 函数会将浮点数向上取整,返回大于或等于原始浮点数的最小整数。
```python
import math
x = 3.14
y = math.ceil(x)
print(y) 输出:4
根据你的需求,可以选择合适的方法进行浮点数的取整操作