# 取整数示例代码
# 使用 int() 函数将浮点数转换为整数
float_num = 3.14
integer_num = int(float_num)
print(integer_num) # 输出: 3
# 使用 // 运算符进行地板除(向下取整)
dividend = 10
divisor = 3
quotient = dividend // divisor
print(quotient) # 输出: 3
# 使用 math.floor() 函数进行向下取整
import math
float_num2 = -3.14
floor_result = math.floor(float_num2)
print(floor_result) # 输出: -4
# 使用 math.ceil() 函数进行向上取整
ceil_result = math.ceil(float_num2)
print(ceil_result) # 输出: -3
# 使用 round() 函数进行四舍五入取整
rounded_result = round(float_num)
print(rounded_result) # 输出: 3
int() 函数:将浮点数转换为整数时,会直接去掉小数部分,不进行四舍五入。// 运算符:进行地板除运算,结果是向下取整的商。math.floor() 函数:返回不大于给定数字的最大整数,即向下取整。math.ceil() 函数:返回不小于给定数字的最小整数,即向上取整。round() 函数:根据四舍五入规则取整。上一篇:python pip 国内源
下一篇:python中[:3]
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站