Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python 取整

作者:最终一次颓废   发布日期:2026-03-23   浏览:38

# 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

大家都在看

python 二维码识别

python excel 库

python时间格式

pythoneval函数用法

列表切片操作python

python读取文件路径

staticmethod在python中有

python 保存json文件

python开发windows应用程序

python中len是什么意思

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站