How to truncate a number in python
To truncate a number in the python language, use the trunc() function of the math library.
import math
math.trunc(x)
The x parameter is the real number to round.
The function returns an integer by truncation.
What is truncation? In mathematics, truncation is a rounding by eliminating all the decimal places of a number.
Examples
Example 1
This script calculates the truncation of the number 3.9
import math
x=3.9
y=math.trunc(x)
print(y)
The script returns the number without the decimal places.
3
Example 2
This script rounds the number 3.1 by truncation.
import math
y=math.trunc(3.1)
print(y)
The output of the script is as follows
3
https://how.okpedia.org/en/python/how-to-truncate-a-number-in-python
Report an error or share a suggestion to enhance this page