round() is Python’s built-in function for rounding floating-point numbers to a specified number of decimal places or to the nearest integer — using banker's rounding (round half to even) for exact .5 cases. In 2026, round() remains essential in data science (formatting metrics, reducing precision in pandas/Polars/Dask DataFrames), financial calculations (money rounding), scientific reporting, and UI display — handling ties consistently, supporting negative ndigits (rounding to tens/hundreds), and integrating seamlessly with NumPy vectorized rounding and string formatting.
Here’s a complete, practical guide to using round() in Python: basic rounding, banker's rounding behavior, negative ndigits, real-world patterns (earthquake magnitude display, financial aggregation, data cleaning), and modern best practices with type hints, precision control, performance, and integration with NumPy/Dask/Polars/pandas/f-strings.
Basic round(number, ndigits=0) — round to nearest integer or specified decimals.
print(round(3.14159)) # 3
print(round(3.14159, 2)) # 3.14
print(round(3.14159, 4)) # 3.1416
print(round(2.675, 2)) # 2.67 (banker's rounding: 2.675 ? 2.68? No, 2.67!)
print(round(-3.5)) # -4 (rounds away from zero on .5)
print(round(-2.5)) # -2 (rounds to even: -2.5 ? -2)
Banker's rounding & ties — rounds to nearest even on exact .5.
print(round(2.5)) # 2 (2.5 ? 2, nearest even)
print(round(3.5)) # 4 (3.5 ? 4, nearest even)
print(round(4.5)) # 4 (4.5 ? 4, nearest even)
print(round(1.5)) # 2 (1.5 ? 2, nearest even)
# Decimal places with ties
print(round(1.225, 2)) # 1.22 (1.225 ? 1.22, nearest even)
print(round(1.235, 2)) # 1.24 (1.235 ? 1.24)
Negative ndigits — round to tens, hundreds, etc. (round to nearest 10^n).
print(round(1234, -1)) # 1230 (nearest ten)
print(round(1234, -2)) # 1200 (nearest hundred)
print(round(5678, -3)) # 6000 (nearest thousand)
print(round(9999, -2)) # 10000 (round up)
Real-world pattern: earthquake magnitude & depth display — clean rounding for reports/tables.
import pandas as pd
df = pd.read_csv('earthquakes.csv')
# Round magnitudes to 1 decimal, depths to nearest 10 km
df['mag_rounded'] = df['mag'].round(1)
df['depth_rounded'] = df['depth'].round(-1) # nearest 10 km
# Summary stats with rounded display
summary = df.groupby('country')['mag'].agg(['min', 'max', 'mean'])
summary_rounded = summary.round(2)
print(summary_rounded)
# Polars: vectorized rounding
import polars as pl
pl_df = pl.from_pandas(df)
rounded_pl = pl_df.with_columns(
pl.col('mag').round(1).alias('mag_rounded'),
pl.col('depth').round(-1).alias('depth_rounded')
)
print(rounded_pl.head())
Best practices for round() in Python & data workflows. Prefer round(x, ndigits) — for display/formatting; use np.around for NumPy arrays. Modern tip: use Polars pl.col('mag').round(2) — fast vectorized rounding; Dask ddf['mag'].round(2). Understand banker's rounding — .5 rounds to nearest even (not always away from zero). Add type hints — def round_mag(mag: float, decimals: int = 1) -> float: return round(mag, decimals). Use negative ndigits — for rounding to tens/hundreds (e.g., round(value, -2) ? nearest 100). Use round() before formatting — f"{round(value, 2):.2f}" avoids floating-point display issues. Handle ValueError — when input not numeric. Use decimal.Decimal — for exact decimal rounding (avoid float precision loss). Use round() in assertions — assert round(mean, 2) == 7.25. Use round() with key — min(lst, key=lambda x: round(x)) (rare). Use round() in binning — int(round(mag)) for magnitude categories. Profile performance — round() is fast; vectorize with NumPy/Polars. Use np.round(arr, decimals) — NumPy equivalent. Use pl.col('x').round(2) — Polars. Use ddf['x'].round(2) — Dask. Use ds.round() — xarray Dataset rounding. Use round(x) in financial apps — but prefer decimal for money. Use round() with quantize() — for precise decimal control.
round(number, ndigits=0) rounds floats to specified decimals or integers — uses banker's rounding for .5 ties, supports negative ndigits. In 2026, use for display formatting, data cleaning, stats rounding, and integrate with pandas/Polars/Dask/NumPy for vectorized rounding. Master round(), and you’ll handle numeric presentation cleanly and accurately in any scientific or reporting workflow.
Next time you need to round a number — use round(). It’s Python’s cleanest way to say: “Make this number neat — to the precision you want, with fair ties.”