all Python 3mathterm
math.trunc
rounding
Cuts off the fractional part — rounding towards zero. math.trunc(2.9) → 2, math.trunc(-2.9) → -2.
Syntax
math.trunc(x)
For positive numbers trunc and floor are indistinguishable; the difference shows up only on negatives: trunc(-3.5) is -3 while floor(-3.5) is -4. On a float trunc does exactly what int(x) does, which is why it rarely appears in beginner code — it is used to state the intent explicitly or to hook into a custom class's __trunc__.
Examples
import math
print(math.trunc(3.9)) # → 3
print(math.trunc(-3.9)) # → -3
print(math.trunc(3.1)) # → 3
print(math.trunc(-3.1)) # → -3
print(math.trunc(0.9)) # → 0
print(math.trunc(-0.5)) # → 0
See also
Open in the full glossarydocs.python.orgReport an inaccuracy