# Python 取整示例
# 1. 使用 int() 函数
# int() 函数会将浮点数向零取整
x = 3.7
result = int(x)
print(f"int({x}) = {result}") # 输出: int(3.7) = 3
# 2. 使用 math.floor() 函数
# math.floor() 函数会将浮点数向下取整
import math
x = 3.7
result = math.floor(x)
print(f"math.floor({x}) = {result}") # 输出: math.floor(3.7) = 3
# 3. 使用 math.ceil() 函数
# math.ceil() 函数会将浮点数向上取整
x = 3.2
result = math.ceil(x)
print(f"math.ceil({x}) = {result}") # 输出: math.ceil(3.2) = 4
# 4. 使用 round() 函数
# round() 函数会根据四舍五入规则取整
x = 3.5
result = round(x)
print(f"round({x}) = {result}") # 输出: round(3.5) = 4
int():直接去掉小数部分,向零取整。math.floor():向下取整,即取不大于该数的最大整数。math.ceil():向上取整,即取不小于该数的最小整数。round():四舍五入取整。上一篇:python文件操作
下一篇:python queue
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站