Python 中的 trunc()
pythonserver side programmingprogramming更新于 2025/8/2 7:08:17
在本教程中,我们将学习 math.trunc() 方法。
方法 math.trunc() 用于截断浮点值。对于正值,它将充当 math.floor() 方法;对于负值,它将充当 math.ceil() 方法。
示例
# 导入数学模块 import math # floor 值 print(math.floor(3.5)) # trunc 为正数 print(math.trunc(3.5))
输出
如果运行上述代码,您将获得类似以下结果。
3 3
示例
# 导入数学模块 import math # ceil 值 print(math.ceil(-3.5)) # trunc 为负数number print(math.trunc(-3.5))
输出
如果运行上述代码,则会得到类似以下的结果。
-3 -3
结论
如果您对本教程有任何疑问,请在评论部分中提及。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

