# 取整数的几种方法
# 方法1: 使用 int() 函数
# int() 函数可以将浮点数转换为整数,它会直接截断小数部分
float_num = 3.14
integer_num = int(float_num)
print(integer_num) # 输出: 3
# 方法2: 使用 math.floor() 函数
# math.floor() 函数返回小于或等于给定数值的最大整数
import math
float_num = 3.99
floor_result = math.floor(float_num)
print(floor_result) # 输出: 3
# 方法3: 使用 math.ceil() 函数
# math.ceil() 函数返回大于或等于给定数值的最小整数
ceil_result = math.ceil(float_num)
print(ceil_result) # 输出: 4
# 方法4: 使用 round() 函数
# round() 函数根据四舍五入规则返回最接近的整数
float_num = 3.5
round_result = round(float_num)
print(round_result) # 输出: 4
int() 函数直接截断小数部分,不进行四舍五入。math.floor() 函数返回小于或等于给定数值的最大整数(向下取整)。math.ceil() 函数返回大于或等于给定数值的最小整数(向上取整)。round() 函数根据四舍五入规则返回最接近的整数。上一篇:python int()
下一篇:python 引用
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站