CKTIS / Python
round()
Arrondit un nombre à une position décimale choisie.
Version : Python 3.14; repository image, production runtime not attested
Syntaxe
round(number, ndigits=None)Arrondit un nombre à une position décimale choisie.
Sans ndigits un float donne l’entier le plus proche et une égalité exacte favorise le voisin pair. Ainsi 2,5, 3,5 et −2,5 donnent 2, 4 et −2. La position −1 arrondit 151 à la dizaine, soit 150.
La représentation binaire peut modifier une égalité apparente.
assert tuple(round(x) for x in (2.5, 3.5, -2.5)) == (2, 4, -2)
assert round(151, -1) == 150 and isinstance(round(2.5, 0), float)
Exemples
print(round(2.5), round(3.5), round(-2.5))
print(round(151, -1))
Résultat
2 4 -2 150
La représentation binaire peut modifier une égalité apparente.