math.trunc
Python math.trunc()方法 (Python math.trunc() method)
math.trunc() method is a library method of math module, it is used to get the truncated integer value of a number, it accepts a number (either an integer or a float) and returns the real value truncated to an integral.
math.trunc()方法是math模块的库方法,用于获取数字的截断整数值,它接受数字(整数或浮点数)并返回被截断为整数的实数值。
Note: If anything is passed except the number, the method returns a type error, if we pass a string – it will return "TypeError: type str doesn't define __trunc__ method"
注意:如果传递了除数字以外的任何内容,则该方法将返回类型错误,如果我们传递一个字符串,则将返回“ TypeError:类型str未定义__trunc__方法”
Syntax of math.trunc() method:
math.trunc()方法的语法:
math.trunc(n)
Parameter(s): n – an integer or a float number.
参数: n-整数或浮点数。
Return value: int – it returns an integer value that is the integral part of the number n.
返回值: int –它返回整数值,该整数值是数字n的整数部分。
Example:
例:
Input:
a = 10.23
# function call
print(math.trunc(a))
Output:
10
Python代码演示math.trunc()方法的示例 (Python code to demonstrate example of math.trunc() method)
# Python code demonstrate example of
# math.trunc() method
# importing math module
import math
# numbers
a = 10
b = 10.23
c = -10
d = -10.67
e = 10.67
# printing the fractional and integer part
# of the numbers by using math.trunc()
print("trunc(a): ", math.trunc(a))
print("trunc(b): ", math.trunc(b))
print("trunc(c): ", math.trunc(c))
print("trunc(d): ", math.trunc(d))
print("trunc(e): ", math.trunc(e))
Output
输出量
trunc(a): 10
trunc(b): 10
trunc(c): -10
trunc(d): -10
trunc(e): 10
Recommended posts
推荐的帖子
翻译自: https://www.includehelp.com/python/math-trunc-method-with-example.aspx
math.trunc