python math.trunc()方法,返回数值整数部分
math.trunc()方法的语法
trunc(x:Real) -> Integral
源码中对trunc()方法的介绍:
Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.
即,python标准库模块math中的trunc()方法使用__trunc__这一maginc方法,截短参数x为最趋近于零的整数,即只返回其整数部分。
参数
x为real number,即为数值类型,比如int或float。
返回值
python int
math.trunc()实例代码
>>> import math
>>> math.trunc(1.9)
1
>>> math.trunc(0.001)
0
>>> math.trunc(-2.9)
-2
>>> math.trunc('3.3')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type str doesn't define __trunc__ method