Python中的trunc()

在本教程中,我们将学习math.trunc()方法。

方法math.trunc()用于截断浮点值。对于正值,它将用作math.floor()方法;对于负值,它将用作math.ceil()方法。

示例

# importing math module
import math
# floor value
print(math.floor(3.5))
# trunc for positive number
print(math.trunc(3.5))

输出结果

如果运行上面的代码,则将得到类似的结果,如下所示。

3
3

示例

# importing math module
import math
# ceil value
print(math.ceil(-3.5))
# trunc for negative number
print(math.trunc(-3.5))

输出结果

如果运行上面的代码,则将得到类似的结果,如下所示。

-3
-3

结论

如果您对本教程有任何疑问,请在评论部分中提及。