菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt Python for 循环 Python while 循环
(追記) (追記ここまで)

Python math.modf() 函数

Python math 模块 Python math 模块


在数值计算中,有时我们需要分离一个数字的整数部分和小数部分。例如,从 3.14 中提取出 3 和 0.14。

math.modf() 就是专门用于执行这一操作的函数,它返回一个元组,包含数字的小数部分整数部分

单词释义: modf 是 "modular fractional" 的缩写,意为"小数部分"。


基本语法与参数

语法格式

import math
math.modf(x)

参数说明

  • 参数: x - 数值(整数或浮点数)

返回值

  • 返回元组 (fractional, integer)
  • fractional: 小数部分,符号与 x 相同
  • integer: 整数部分(向零取整)

实例

示例 1:基础用法

实例

import math

result = math.modf(3.14159)
print("math.modf(3.14159) =", result)
fractional, integer = math.modf(3.14159)
print(f"小数部分: {fractional}, 整数部分: {integer}")

运行结果:

math.modf(3.14159) = (0.14158999999999988, 3.0)
小数部分: 0.14158999999999988, 整数部分: 3.0

示例 2:处理负数

实例

import math

print("math.modf(-3.14) =", math.modf(-3.14))
print("math.modf(-0.5) =", math.modf(-0.5))
fractional, integer = math.modf(-3.14)
print(f"小数部分: {fractional}, 整数部分: {integer}")

运行结果:

math.modf(-3.14) = (-0.14000000000000012, -3.0)
math.modf(-0.5) = (-0.5, -0.0)
小数部分: -0.14, 整数部分: -3.0

示例 3:处理整数

实例

import math

print("math.modf(5) =", math.modf(5))
print("math.modf(0) =", math.modf(0))
print("math.modf(-8) =", math.modf(-8))

运行结果:

math.modf(5) = (0.0, 5.0)
math.modf(0) = (0.0, 0.0)
math.modf(-8) = (-0.0, -8.0)

示例 4:实际应用 - 金额分离

实例

import math

# 分离金额:元、角、分
price = 12.58
fractional, integer = math.modf(price)
yuan = int(integer)
jiao = int(fractional * 10)
fen = int((fractional * 10 - jiao) * 10)
print(f"价格 {price} 元 = {yuan} 元 {jiao} 角 {fen} 分")

# 时间转换:小时转时:分:秒
hours = 73.5
fractional_hours, whole_hours = math.modf(hours)
total_minutes = fractional_hours * 60
fractional_minutes, whole_minutes = math.modf(total_minutes)
seconds = fractional_minutes * 60
print(f"{hours} 小时 = {int(whole_hours)} 时 {int(whole_minutes)} 分 {int(seconds)} 秒")

运行结果:

价格 12.58 元 = 12 元 5 角 7 分
73.5 小时 = 73 时 30 分 0 秒

注意事项

  • 返回的整数部分是浮点数类型,如 3.0 而非 3
  • 整数部分采用"向零取整"(truncation),不同于 floor
  • 浮点数存在精度误差,结果可能会有微小偏差

Python math 模块 Python math 模块

AI 思考中...

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /