GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python round Function - Complete Guide Python round FunctionLast modified April 11, 2025
This comprehensive guide explores Python's Basic Definitions
The Key characteristics: works with numbers, follows "round half to even" rule (bankers rounding), and returns a float when precision is specified. Basic Rounding Usage
Here's simple usage with different numbers showing how basic_round.py
# Rounding to nearest integer print(round(3.14)) # 3 print(round(3.5)) # 4 (note: bankers rounding) print(round(3.6)) # 4 print(round(-3.14)) # -3 print(round(-3.5)) # -4 # Rounding with precision print(round(3.14159, 2)) # 3.14 print(round(3.14159, 3)) # 3.142
This example shows With precision argument, it rounds to specified decimal places. The function returns a float even when rounding to whole numbers with precision specified. Bankers Rounding ExplainedPython uses "round half to even" (bankers rounding) which minimizes bias in statistical operations. This example demonstrates the behavior. bankers_rounding.py
print(round(2.5)) # 2 (rounds to even) print(round(3.5)) # 4 (rounds to even) print(round(4.5)) # 4 (rounds to even) print(round(5.5)) # 6 (rounds to even) print(round(-2.5)) # -2 print(round(-3.5)) # -4 Bankers rounding rounds 0.5 cases to the nearest even number. This differs from traditional "round half up" but provides better statistical properties. The same rule applies to negative numbers. Note how -2.5 rounds to -2 (nearest even number) rather than -3. Precision ControlThe precision argument controls how many digits are kept after the decimal point. This example shows various precision levels. precision.py
pi = 3.141592653589793 print(round(pi, 0)) # 3.0 print(round(pi, 1)) # 3.1 print(round(pi, 2)) # 3.14 print(round(pi, 3)) # 3.142 print(round(pi, 4)) # 3.1416 # Negative precision rounds to left of decimal print(round(12345, -1)) # 12340 print(round(12345, -2)) # 12300 print(round(12345, -3)) # 12000 Positive precision values round after the decimal point, while negative values round to the left of the decimal (tens, hundreds, etc.). Note that precision=0 returns a float (3.0) rather than an integer, unlike when no precision is specified (which returns an integer). Floating Point ConsiderationsDue to floating point representation, some results may be surprising. This example demonstrates cases where rounding might not behave as expected. floating_point.py
print(round(2.675, 2)) # 2.67 (not 2.68) print(round(0.1 + 0.2, 1)) # 0.3 (as expected) # Explanation from decimal import Decimal print(Decimal(2.675)) # Shows actual stored value The first example shows 2.675 rounding down to 2.67 instead of up to 2.68. This happens because 2.675 cannot be represented exactly in binary floating point.
For precise decimal arithmetic, consider using the Practical Application: Financial Calculations
This example demonstrates using financial.py
def calculate_interest(principal, rate, years):
return round(principal * (1 + rate/100)**years, 2)
def calculate_monthly_payment(principal, rate, months):
monthly_rate = rate / 100 / 12
payment = principal * monthly_rate / (1 - (1 + monthly_rate)**-months)
return round(payment, 2)
print(calculate_interest(1000, 5, 10)) # 1628.89
print(calculate_monthly_payment(10000, 7.5, 60)) # 200.38
Financial calculations often require rounding to 2 decimal places for currency.
The
For banking applications where exact decimal representation is crucial, the
Best Practices
Source ReferencesAuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |