# Python取整示例
# 使用 int() 函数
# int() 函数会将浮点数向下取整,即去掉小数部分
num1 = 3.7
result1 = int(num1)
print(f"int({num1}) = {result1}") # 输出: int(3.7) = 3
# 使用 math.floor() 函数
# math.floor() 函数会将浮点数向下取整到最接近的整数
import math
num2 = -2.3
result2 = math.floor(num2)
print(f"math.floor({num2}) = {result2}") # 输出: math.floor(-2.3) = -3
# 使用 math.ceil() 函数
# math.ceil() 函数会将浮点数向上取整到最接近的整数
num3 = 4.1
result3 = math.ceil(num3)
print(f"math.ceil({num3}) = {result3}") # 输出: math.ceil(4.1) = 5
# 使用 round() 函数
# round() 函数会根据四舍五入规则取整
num4 = 2.5
result4 = round(num4)
print(f"round({num4}) = {result4}") # 输出: round(2.5) = 2
# 使用 // 运算符
# // 运算符会进行地板除法,即返回商的整数部分
num5 = 10
num6 = 3
result5 = num5 // num6
print(f"{num5} // {num6} = {result5}") # 输出: 10 // 3 = 3
上一篇:python break
下一篇:python 多进程
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站