The built-in round() function rounds a number to a given number of decimal places, or to the nearest integer when you omit the second argument. Be aware that Python uses banker's rounding (round half to even), so round(2.5) returns 2, not 3, and floating-point storage can make results such as round(2.675, 2) look wrong.
For money and any case where "half rounds up" is a business rule, use the decimal module instead of round() on floats.
number is an int, float, or any object that implements __round__() (for example Decimal and Fraction).ndigits is optional. When it is omitted or None, round() returns an int. When you pass an integer, the result has the same type as number.ndigits can be negative, which rounds to tens, hundreds and so on.A common surprise is that round(2.5, 0) returns a float 2.0 while round(2.5) returns the int 2. Leave ndigits out when you want an integer.
A negative ndigits rounds to the left of the decimal point: -1 means the nearest 10, -2 the nearest 100. This works on integers too, which makes it handy for bucketing values.
Python 3 rounds exact halves to the nearest even number. This is the IEEE 754 default and it avoids the upward bias you get when many halves are always rounded up.
The same applies to integer inputs with negative digits: round(15, -1) and round(25, -1) both return 20. Python 2 rounded halves away from zero, so old tutorials that show round(2.5) == 3 are wrong for Python 3.
An example that surprises people is averaging two numbers:
Most decimal fractions cannot be stored exactly in binary floating point. round() operates on the stored value, not on what you typed.
The float 2.675 is actually stored as 2.67499999999999982..., so it is below the halfway point and rounds down. This is expected behaviour, not a bug in round().
When you need "round half up" or exact decimal arithmetic, use decimal and keep the values as Decimal from the start.
Build the Decimal from a string (Decimal("2.675")), not from a float, otherwise you inherit the float error.
Prefer math.pi over a typed constant in real code. For output only, an f-string such as f"{value:.2f}" formats the number without changing it.
round() never rounds in a fixed direction. Use math.floor() to round down and math.ceil() to round up:
numpy.round() and Series.round() also use round-half-to-even, so they agree with the built-in function.
| Need | Use |
|---|---|
| Nearest value, halves to even | round(x, n) |
| Always round down | math.floor(x) |
| Always round up | math.ceil(x) |
| Drop the fraction toward zero | math.trunc(x) or int(x) |
| Exact decimal money, half up | Decimal.quantize(..., rounding=ROUND_HALF_UP) |
| Only format for display | f-string such as f"{x:.2f}" |
Python 3 uses round half to even, so exact halves go to the nearest even integer. round(3.5) returns 4, which keeps rounding errors balanced across many values.
The float 2.675 is stored slightly below 2.675, so it rounds down. Use Decimal("2.675") with ROUND_HALF_UP if you need 2.68.
Pass a negative ndigits: round(456, -1) returns 460 and round(1234, -2) returns 1200.
Without ndigits it returns an int. With ndigits it returns the same type as the input, so a float stays a float.
round(number, ndigits) rounds to decimal places, or to tens and hundreds with negative digits. It uses banker's rounding and works on stored binary floats, so use Decimal for money, math.floor() and math.ceil() for fixed direction, and f-strings when you only need formatted output.