How To Round In Python?

Use `round(number)` to round to the nearest integer

Use `round(number, ndigits)` to round to a specific number of decimal places

Example: `round(3.14159, 2)` returns `3.14`

Example: `round(3.5)` returns `4`

Example: `round(2.5)` returns `2`

Use `math.floor(number)` to round down

Use `math.ceil(number)` to round up

Use `int(number)` to truncate toward zero

Use formatted strings like `f”{number:.2f}”` to display rounded values

Use `decimal.Decimal` for precise rounding control

Use `numpy.round()` when working with NumPy arrays

Suggested for You

Trending Today