在Python中,取整的方法主要有以下几种:
1. 向下取整(floor):
使用 `math.floor()` 函数可以将浮点数向下取整为最接近它的整数。
import matha = 3.6b = math.floor(a)print(b) 输出 3
2. 向上取整(ceil):
使用 `math.ceil()` 函数可以将浮点数向上取整为最接近它的整数。
import mathf = 11.2print(math.ceil(f)) 向上取整
3. 四舍五入取整:

使用 `round()` 函数可以实现四舍五入取整。
print(round(3.25)) 四舍五入print(round(4.85)) 四舍五入
4. 使用内置的 `int()` 函数:
`int()` 函数可以将浮点数向下取整为最接近它的整数。
a = 3.75print(int(a)) 向下取整
5. 使用 `math.modf()` 函数分别取整数部分和小数部分:
import matha = 4.25integer_part, fractional_part = math.modf(a)print(integer_part) 输出整数部分print(fractional_part) 输出小数部分
