English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Funzioni predefinite di Python
La funzione round() restituisce un numero decimale arrotondato a una specifica posizione decimale.
La sintassi di round() è:
round(number, ndigits)
round() ha due parametri:
number - Il numero da arrotondare
ndigits (opzionale) - Il numero arrotondato al numero dato; predefinito a 0
Se non viene fornito ndigits, round() restituisce l'intero più vicino al numero fornito.
Se viene fornito ndigit, round() restituisce il numero arrotondato a ndigit.
# Il numero è un numero intero print(round(10)) # Il numero è un numero decimale print(round(10.7)) # Il numero è un numero decimale print(round(5.5))
Risultato di output
10 11 6
print(round(2.665, 2)) print(round(2.675, 2))
Risultato di output
2.67 2.67
Nel programma, potresti pensare che 2.675 dovrebbe arrotondare a 2.68. Questo non è un bug. È considerato un metodo di arrotondamento standard.