Python round(): Rounding and Truncation
Round to a nearby value
For a finite float, round(number) returns a nearest integer. round(number, ndigits) rounds to the requested decimal digit position and returns a float when the input is a float. Use a positive digit count for places after the decimal point. This changes the numeric result; it is not a request for a fixed number of displayed characters.
print(round(7.6))print(round(7.6, 0))print(round(1.375, 2))With no digit count the result is 8; with zero digits it is 8.0. In the third line, 1.375 is exactly representable in binary, and rounding to hundredths produces 1.38.
What happens exactly halfway?
Python chooses the even neighbor for an exact tie. That is why 2.5 becomes 2 and 3.5 becomes 4. A decimal-looking halfway value may not be an exact tie after binary float representation; do not infer a different rounding rule from such a case.
Rounding is not truncation
print(round(-2.75))print(int(-2.75))print(round(2.675, 2))The first two results differ because round chooses the nearest integer while int truncates toward zero. The third is 2.67 on the tested runtime because the stored float is slightly below the exact decimal halfway value. Decimal rounding policies for monetary work need an explicit numeric model, not just round on binary floats.
Practice
Predict round(4.5), round(5.5) and int(-5.75). Run them and explain the different rules.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Rounding and truncation
Check your understanding of rounding.
What is printed?
print(round(4.5))What is printed?
print(round(-3.5))What is printed?
print(round(5.75, 0))Which conversion discards the fractional part of a finite float toward zero?
Why may a decimal-looking halfway input round differently than expected?