math.trunc_带有Python示例的math.trunc()方法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值