Home » Python

math.trunc() method with example in Python

Python math.trunc() method: Here, we are going to learn about the math.trunc() method with example in Python.
Submitted by IncludeHelp, on April 17, 2019

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.

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"

Syntax of math.trunc() method:

    math.trunc(n)

Parameter(s): n – an integer or a float number.

Return value: int – it returns an integer value that is the integral part of the number n.

Example:

    Input:
    a = 10.23

    # function call
    print(math.trunc(a))

    Output:
    10

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.