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.
In case of positive number
# 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
# 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:-

This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- numpy.trunc() in Python
- Python - PyTorch trunc() method
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
Improved By : ASuresh

