Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / Python

math.trunc

Descarta a parte fracionária e devolve um int para um float.

Versão: Python 3.14; repository image, production runtime not attested

Sintaxe

math.trunc(x, /)

Descarta a parte fracionária e devolve um int para um float.

Tanto 2,9 como −2,9 aproximam-se de zero, dando respetivamente 2 e −2. Para valores negativos o resultado coincide com ceil.

Um tipo numérico pode definir __trunc__.

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

Exemplos

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

Resultado

2
-2

Um tipo numérico pode definir `__trunc__`.

Fontes