Python trunc() Function Example

Python trunc() is an inbuilt math function that behaves like a floor() when the given input is positive, on the other hand, it works like ceil() when then given input is negative. We know that floor() function is used to round down towards negative infinity, and ceil() is used to round up towards positive infinity. But here, the trunc() function is a combination of both.

Python trunc()

Python trunc() method or truncate function is one of the Python Math functions used to remove the decimal values from particular expression and return the integer value. This Function is under the python math library, so we have to import math when we want to use it.

Syntax

math.trunc(number)

This Function takes only one argument. Here, a number can be positive or negative.

Return Value

This function returns an integer positive or negative depending upon the given input. But it always tries to round up or down towards zero.

Programming Example

See the following code example.

# Program to show the working of trunc
# Using positive numbers
import math

# Pre defined value
n = 10.5
# Showing working of floor
print("Floor is: ", math.floor(n))
# Showing working of ceil
print("Ceil is: ", math.ceil(n))
# Showing working of trunc
print("Trunc is: ", math.trunc(n))

Output

Floor is:  10
Ceil is:  11
Trunc is:  10

In the above program, we have given value to n, and then we have called all three types of functions to make you understand. We can see that the value returned from the floor() and trunc() are the same because trunc() is downgrading the number and floor() too.

Python trunc() with a negative value

See the following code.

# Program to show working of trunc
# Using positive numbers
import math

# Taking imput from user
n = float(input("Enter a negative number: "))
# Showing working of floor
print("Floor is: ", math.floor(n))
# Showing working of ceil
print("Ceil is: ", math.ceil(n))
# Showing working of trunc
print("Trunc is: ", math.trunc(n))

Output

# Program to show working of trunc
# Using positive numbers
import math

# Taking imput from user
n = float(input("Enter a negative number: "))
# Showing working of floor
print("Floor is: ", math.floor(n))
# Showing working of ceil
print("Ceil is: ", math.ceil(n))
# Showing working of trunc
print("Trunc is: ", math.trunc(n))

Python trunc() with Tuple and List value

Okay, let’s take an example.

# app.py

import math

Tup = (21.98, 19.26, 11.05, -40.95 , 50.85) # Tuple Declaration
Lis = [-21.98, 32.65, -39.29, -46.15 , -39.97] # List Declaration

print('Python TRUNC() Function on Positive Decimal = %d' %math.trunc(21.98763))
print('Python TRUNC() Function on Negative Decimal = %d' %math.trunc(-15.48476))

print('Python TRUNC() Function on Tuple Item = %d' %math.trunc(Tup[2]))
print('Python TRUNC() Function on Tuple Item = %d' %math.trunc(Tup[4]))
print('Python TRUNC() Function on List Item = %d' %math.trunc(Lis[2]))
print('Python TRUNC() Function on List Item = %d' %math.trunc(Lis[4]))

print('Python TRUNC() Function on Multiple Number = %d' %math.trunc(21 + 19 - 40.6578))

print('Python TRUNC() Function on String Value = ', math.trunc('2.95'))

Output

python3 app.py
Python TRUNC() Function on Positive Decimal = 21
Python TRUNC() Function on Negative Decimal = -15
Python TRUNC() Function on Tuple Item = 11
Python TRUNC() Function on Tuple Item = 50
Python TRUNC() Function on List Item = -39
Python TRUNC() Function on List Item = -39
Python TRUNC() Function on Multiple Number = 0
Traceback (most recent call last):
  File "app.py", line 16, in <module>
    print('Python TRUNC() Function on String Value = ', math.trunc('2.95'))
TypeError: type str doesn't define __trunc__ method

Conclusion

Within this Python truncate function example,

  1. We used the python trunc() method directly on both the Positive decimals and negative decimals using Python input().
  2. Then, We used the trunc() method on Tuple and List items. If you observe the above output, the trunc() function is working correctly on them.
  3. Then in the same program, we tried Python trunc() Function with multiple values.
  4. In the last statement, we tried Python truncate Function on the String value. As we said before, it returns TypeError: type str doesn’t define __trunc__ method as output.

See also

Python absolute value

Python fabs()

Python factorial

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.