math
round()
Rounds a number to ndigits decimal places. When ndigits is None (default), returns an int rounded to nearest integer using banker's rounding.
round(number, ndigits=None) Parameters
| Parameter | Purpose |
|---|---|
| number | Value to round |
| ndigits | Decimal places (negative rounds to tens/hundreds); None returns int |
Examples
round(3.7) Returns 4 (int)
round(2.5) Returns 2 (banker's rounding — nearest even)
round(3.14159, 2) Returns 3.14
round(1234, -2) Returns 1200
Gotcha
Uses banker's rounding (round-half-to-even) — round(0.5) is 0, round(2.5) is 2. Also affected by float representation: round(2.675, 2) returns 2.67.
Related built-ins
abs()
Returns the absolute value of a number. For complex numbers, returns the magnitude.
int()
Constructs an integer from a number or string. When x is a string, base selects the numeric base (0 auto-detects from prefix).
float()
Constructs a floating-point number from a number or string. Accepts 'inf', '-inf', 'nan' (case-insensitive).