Python round() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, round(number) returns the nearest int, and round(number, ndigits) rounds to ndigits decimal places and keeps the number's type, so a float stays a float.

Exact halves are rounded to the nearest even digit (“round half to even”, also called banker's rounding): round(2.5) is 2 and round(3.5) is 4. Because floats are stored in binary, some values that look like halves are slightly below or above, so round(2.675, 2) gives 2.67.

round() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Leave out ndigits to get an int; pass it to keep decimals.

python
whole = round(number)
two_places = round(number, 2)
CallWhat it does
round(3.7)Returns 4 (an int).
round(2.5)Returns 2: halves go to the even neighbour.
round(3.14159, 2)Returns 3.14 (a float).
round(1234, -2)Returns 1200: negative ndigits rounds to tens, hundreds, ...

For display only, an f-string such as f"{x:.2f}" formats without changing the value.

Library declarations
python
round(number, ndigits=None)
# calls number.__round__(ndigits)

Parameters

ParameterDescription
numberRequired. An int, float, Decimal, Fraction, or any object with __round__().
ndigitsOptional. Number of decimal places. None (default) returns an int; a negative value rounds to the left of the decimal point.

Return Value

Return ValueDescription
intWhen ndigits is omitted or None.
same type as numberWhen ndigits is given (float for a float, Decimal for a Decimal).

Python Examples

Example 1 — Round Half to Even in Action

Round five exact halves and watch which way each one goes.

python
for value in [0.5, 1.5, 2.5, 3.5, -2.5]:
    print(value, "->", round(value))

How It Works

  1. Exact halves. All five values sit exactly halfway between two integers, and binary floats store them exactly.
  2. Even neighbour wins. 0.5 goes to 0 and 2.5 goes to 2, while 1.5 and 3.5 go up to the even numbers 2 and 4.
  3. Negatives too. -2.5 rounds to -2, the even choice between -3 and -2.
  4. Why this rule. Always rounding halves up adds a small upward bias over many values; half-to-even cancels it out.

Example 2 — Decimal Places, Tens, and the 2.675 Surprise

Round to places on both sides of the decimal point and check the types that come back.

python
price = 19.98765

print(round(price, 2))
print(round(price, 0))
print(round(2.675, 2))
print(round(1234567, -3))
print(type(round(price)).__name__, type(round(price, 2)).__name__)

How It Works

  1. Two places. round(19.98765, 2) keeps two decimals: 19.99.
  2. ndigits=0 keeps a float. round(price, 0) returns 20.0; only omitting ndigits gives an int.
  3. Binary surprise. 2.675 is stored as 2.67499999..., slightly below the half, so it rounds down to 2.67.
  4. Negative ndigits. -3 rounds to the nearest thousand: 1234567 becomes 1235000.

Example 3 — Round Money Correctly with Decimal

Compare the default half-even rounding of a Decimal with explicit half-up rounding, and format without rounding the value.

python
from decimal import Decimal, ROUND_HALF_UP

amount = Decimal("2.665")
print(round(amount, 2))
print(amount.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))
print(f"{2.5:.0f}", f"{3.14159:.3f}")

How It Works

  1. Exact decimal. Decimal("2.665") is stored exactly, so it really is a half at the third place.
  2. round() uses half-even. The default decimal context rounds 2.665 to 2.66, because 6 is the even digit.
  3. Choose the rule. quantize(..., rounding=ROUND_HALF_UP) applies school-style rounding and gives 2.67.
  4. Formatting follows round(). f"{2.5:.0f}" prints 2; .3f shows three places without changing the stored value.

Frequently Asked Questions

Python 3 rounds exact halves to the nearest even number (banker's rounding). round(2.5) is 2 and round(3.5) is 4. This avoids a bias toward larger numbers.
The float 2.675 is actually stored as 2.67499999999999982236431605997495353221893310546875, which is below the halfway point, so it rounds down.
Without ndigits it returns an int. With ndigits (even 0) it returns the same type as the input, so round(3.7, 0) is 4.0.
Use decimal.Decimal with quantize(Decimal("0.01"), rounding=ROUND_HALF_UP), or math.floor(x + 0.5) for non-negative numbers when exactness is not critical.
Use math.ceil() to round up and math.floor() to round down. int() truncates toward zero.

Next: set()

Build an unordered collection of unique items.

set() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful