Worldscope

Python round()

Palavras-chave:

Publicado em: 03/08/2025

Understanding Python's round() Function

The round() function in Python is used to round a number to a specified number of decimal places. This article explores how round() works, its behavior, and some alternative approaches for rounding numbers in Python.

Fundamental Concepts / Prerequisites

Before diving into the details of round(), it's helpful to have a basic understanding of floating-point numbers in Python and the concept of decimal places. Familiarity with Python's data types, especially float and int, is also essential. Also, an understanding of how computers store floating-point numbers (using formats like IEEE 754) can explain some unexpected behaviors of round().

Core Implementation

The round() function takes one or two arguments: the number to round and an optional number of decimal places to round to. If the second argument is omitted, it rounds to the nearest integer. Let's see an example:


# Example of using round()

number = 3.14159
rounded_number = round(number, 2) # Round to 2 decimal places
print(rounded_number) # Output: 3.14

integer_rounded = round(number) # Round to nearest integer
print(integer_rounded) # Output: 3

#Demonstrating the default rounding rule of round()
number_up = 2.5
number_down = 3.5

print(round(number_up)) # Output: 2 (rounds to even number)
print(round(number_down)) # Output: 4 (rounds to even number)

number = 5.5
print(round(number)) # Output: 6

Code Explanation

The first example demonstrates rounding 3.14159 to two decimal places using round(number, 2), which results in 3.14. The second example shows rounding the same number to the nearest integer using round(number), resulting in 3. Notice that when only the number is passed to the function round returns an int. Finally, the code shows Python's default rounding scheme: "rounding to even" (also known as banker's rounding). If a number is exactly halfway between two integers, it's rounded to the nearest *even* integer. This helps to reduce bias when rounding a large number of values.

Complexity Analysis

The round() function has a time complexity of O(1), meaning its execution time does not depend on the magnitude of the input number or the number of decimal places. This is because the underlying implementation involves a fixed number of arithmetic operations. The space complexity is also O(1), as it uses a constant amount of memory regardless of the input.

Alternative Approaches

Another approach to rounding numbers is using the math.floor() and math.ceil() functions from the math module. However, these functions always round down or up to the nearest integer, respectively, so achieving a specific number of decimal places requires a bit more work and is typically more complex than using round(). For instance:


import math

number = 3.14159
rounded_down = math.floor(number * 100) / 100  # Round down to 2 decimal places
print(rounded_down) # Output: 3.14

rounded_up = math.ceil(number * 100) / 100  # Round up to 2 decimal places
print(rounded_up) # Output: 3.15

Using math.floor or math.ceil requires multiplying the number by a power of ten to shift the decimal places, rounding with floor/ceil, and then dividing back to the original scale. This is more verbose than using round() but can be useful if you specifically need to round down or up.

Conclusion

The round() function is a simple and efficient way to round numbers in Python. Understanding its behavior, particularly the "rounding to even" rule, is important for avoiding unexpected results. While alternative approaches exist, round() often provides the most concise and readable solution for general rounding needs. It is important to remember though, that due to the limitations of floating-point representation, the results may not always be exactly as expected.