CKTIS / Python
math.trunc
Odřízne desetinnou část a pro float vrátí int.
Verze: Python 3.14; repository image, production runtime not attested
Syntaxe
math.trunc(x, /)Odřízne desetinnou část a pro float vrátí int.
Hodnoty 2,9 i −2,9 se přiblíží nule a dají 2 a −2. Pro záporné hodnoty se výsledek shoduje s ceil.
Číselný typ může definovat __trunc__.
import math
assert math.trunc(-2.9) == -2 and math.floor(-2.9) == -3
assert type(math.trunc(2.9)) is int
Příklady
import math
print(math.trunc(2.9))
print(math.trunc(-2.9))
Výsledek
2 -2
Číselný typ může definovat `__trunc__`.