A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/g-fact-35-truncate-in-python/ below:

trunc() in Python - GeeksforGeeks

trunc() in Python

Last Updated : 20 Feb, 2023

Truncate in Python

There are many built-in modules in python. Out of these module there is one interesting module known as math module which have several functions in it like, ceil, floor, truncate, factorial, fabs, etc.

Out of these functions there is an interesting function called truncate which behaves as a ceiling function for negative number and floor function for positive number.

Time Complexity: O(1)

Auxiliary Space: O(1)
 

Python3
# Python program to show output of floor(), ceil()
# truncate() for a positive number.
import math
print (math.floor(3.5)) # floor
print (math.trunc(3.5)) # work as floor
print (math.ceil(3.5))  # ceil

Output:

3.0
3
4.0


In case of negative number
 

Python3
# Python program to show output of floor(), ceil()
# truncate() for a negative number.
import math
print (math.floor(-3.5)) # floor 
print (math.trunc(-3.5)) # work as ceil
print (math.ceil(-3.5))  # ceil

Output:

-4.0
-3
-3.0


This is because the ceiling function is used to round up, i.e., towards positive infinity and floor function is used to round down, i.e., towards negative infinity. 
 

But the truncate function is used to round up or down towards zero.
Diagrammatic representation of truncate function:- 
 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4