🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

round()

AI & DATA SCIENCE // round

round() rounds a number to a given number of decimal places (or to the nearest whole number by default), using round-half-to-even, also called banker's rounding.

Syntax

round(number, ndigits=None)

Deep Dive Course

round(x) with no second argument rounds to the nearest integer and returns an int; round(x, n) rounds to n decimal places and returns a float. Python uses round-half-to-even for exact ties, meaning a value exactly halfway between two integers rounds toward whichever is even — this reduces statistical bias compared to always rounding up, but it surprises people used to the classic-rounding convention taught in school.

1Understanding round()

round(x) with no second argument rounds to the nearest integer and returns an int; round(x, n) rounds to n decimal places and returns a float. Python uses round-half-to-even for exact ties, meaning a value exactly halfway between two integers rounds toward whichever is even — this reduces statistical bias compared to always rounding up, but it surprises people used to the classic-rounding convention taught in school.

💡

round() operates on the actual binary value of a float, so rounding a value like 2.675 to 2 decimal places can give an unexpected result, because that value isn't exactly representable in binary — this is a floating-point precision issue, not a bug in round() itself.

editor.html
print(round(3.14159, 2))
print(round(7.5))
print(round(2.5))
localhost:3000

2Practical Example

Here is a real-world application of round() showing how it is used in production Python code.

editor.html
from decimal import Decimal, ROUND_HALF_UP
price = Decimal("2.675")
rounded = price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(rounded)
localhost:3000

3Best Practices

Follow these guidelines when working with round():

1. Use the decimal module with ROUND_HALF_UP instead of round() when you need classic, predictable rounding, such as for money

2. Remember round(x, n) still returns a float, which can carry the same floating-point representation quirks as any other float

3. Don't assume round() always rounds a tie upward — verify behavior on ties if it matters for your use case

⚠️

Tip: round() operates on the actual binary value of a float, so rounding a value like 2.675 to 2 decimal places can give an unexpected result, because that value isn't exactly representable in binary — this is a floating-point precision issue, not a bug in round() itself.

editor.html
print(round(3.14159, 2))
print(round(7.5))
print(round(2.5))
localhost:3000

Examples

Example 01Basic Usage
print(round(3.14159, 2))
print(round(7.5))
print(round(2.5))
Example 02Advanced Example
from decimal import Decimal, ROUND_HALF_UP
price = Decimal("2.675")
rounded = price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(rounded)

Best Practices

  • Use the decimal module with ROUND_HALF_UP instead of round() when you need classic, predictable rounding, such as for money
  • Remember round(x, n) still returns a float, which can carry the same floating-point representation quirks as any other float
  • Don't assume round() always rounds a tie upward — verify behavior on ties if it matters for your use case

Interview Question

Why does rounding 2.5 return 2 instead of 3 in Python?

Hint: It's a deliberate rounding rule, not a bug.

Python's round() uses round-half-to-even, also called banker's rounding: when a value is exactly halfway between two integers, it rounds to whichever one is even. So 2.5 rounds to 2, while 3.5 rounds to 4. This convention reduces cumulative rounding bias across many roundings compared to always rounding a tie up, and it matches the IEEE-754 standard rounding rule.

Exercises

MediumPractice using round() in a real scenario.
View Solution
print(round(3.14159, 2))
print(round(7.5))
print(round(2.5))

Frequently Asked Questions

Why does rounding 2.5 return 2 instead of 3 in Python?

Python's round() uses round-half-to-even, also called banker's rounding: when a value is exactly halfway between two integers, it rounds to whichever one is even. So 2.5 rounds to 2, while 3.5 rounds to 4. This convention reduces cumulative rounding bias across many roundings compared to always rounding a tie up, and it matches the IEEE-754 standard rounding rule.

Related Functions

decimal.Decimalmath.floor()math.ceil()