Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / Python

math.trunc

Odreže desatinnú časť a pre float vráti int.

Verzia: Python 3.14; repository image, production runtime not attested

Syntax

math.trunc(x, /)

Odreže desatinnú časť a pre float vráti int.

Hodnoty 2,9 aj −2,9 sa priblížia k nule a dajú 2 a −2. Pre záporné hodnoty sa výsledok zhoduje s ceil.

Číselný typ môže definovať __trunc__.

import math
assert math.trunc(-2.9) == -2 and math.floor(-2.9) == -3
assert type(math.trunc(2.9)) is int

Príklady

import math
print(math.trunc(2.9))
print(math.trunc(-2.9))

Výsledok

2
-2

Číselný typ môže definovať `__trunc__`.

Zdroje