Python

Python round() Function

The round() function is used to round a number to a specified number of decimal places.

It is commonly used for:

  • Decimal numbers
  • Calculating averages
  • Displaying prices
  • Data analysis
  • Financial calculations
  • Machine learning results

Syntax

round(number)

or

round(number, ndigits)

Where:

  • number → The number you want to round
  • ndigits → Number of decimal places to keep

1. Basic Example

number = 10.6

result = round(number)

print(result)

Output:

11

2. Rounding Down

number = 10.4

print(round(number))

Output:

10

3. Rounding to Decimal Places

number = 10.4567

result = round(number, 2)

print(result)

Output:

10.46

Here:

round(number, 2)

means keep 2 decimal places.

4. One Decimal Place

number = 15.678

print(round(number, 1))

Output:

15.7

5. Three Decimal Places

number = 15.67891

print(round(number, 3))

Output:

15.679

6. Rounding to Zero Decimal Places

number = 15.678

print(round(number, 0))

Output:

15.0

Notice that specifying 0 decimal places returns a floating-point result.

Compare:

print(round(15.678))

Output:

16

7. Negative Numbers

round() also works with negative numbers.

number = -15.678

print(round(number, 2))

Output:

-15.68

8. Rounding a List of Numbers

You can use map() with round().

numbers = [10.456, 20.789, 30.123]

result = map(
    lambda x: round(x, 2),
    numbers
)

print(list(result))

Output:

[10.46, 20.79, 30.12]

Since you already learned map(), this is a useful combination.

9. round() with map() Directly

When the number of decimal places is fixed, round can be combined with functools.partial.

But for beginners, the lambda approach is clearer:

numbers = [1.234, 5.678, 9.876]

result = list(
    map(
        lambda x: round(x, 2),
        numbers
    )
)

print(result)

Output:

[1.23, 5.68, 9.88]

10. Calculate Average and Round It

This is one of the most common uses.

marks = [75, 82, 68, 91, 87]

average = sum(marks) / len(marks)

print(average)

Output:

80.6

You can round it:

marks = [75, 82, 68, 91, 88]

average = sum(marks) / len(marks)

print(round(average, 2))

Output:

80.8

11. Calculate Percentage

obtained = 425 total = 500

percentage = (obtained / total) * 100

print(round(percentage, 2))

Output:

85.0

12. Calculate Average with Two Decimal Places

marks = [75, 82, 67, 91, 86, 73]

average = sum(marks) / len(marks)

result = round(average, 2)

print("Average:", result)

Output:

Average: 79.0

13. Rounding Prices

price = 1999.987

final_price = round(price, 2)

print(final_price)

Output:

1999.99

This is useful when displaying calculated prices.

14. Calculate Tax

price = 1000 tax_rate = 13

tax = price * tax_rate / 100

print("Tax:", round(tax, 2))

Output:

Tax: 130.0

15. Calculate Total Price

price = 1499.99 quantity = 3

total = price * quantity

print("Total:", round(total, 2))

Output:

Total: 4499.97

16. round() with Division

Division often produces many decimal places.

result = 10 / 3

print(result)

Output:

3.3333333333333335

Use round():

result = 10 / 3

print(round(result, 2))

Output:

3.33

17. round() with sum()

numbers = [10.25, 20.75, 30.15]

total = sum(numbers)

print(round(total, 2))

Output:

61.15

18. round() with min() and max()

numbers = [10.456, 20.789, 5.123, 30.987]

minimum = round(min(numbers), 2)
maximum = round(max(numbers), 2)

print("Minimum:", minimum)
print("Maximum:", maximum)

Output:

Minimum: 5.12
Maximum: 30.99

19. Rounding Values from filter()

You can combine filter() and round().

numbers = [10.456, 5.234, 20.789, 2.345]

filtered = filter(
    lambda x: x > 10,
    numbers
)

result = [
    round(x, 2)
    for x in filtered
]

print(result)

Output:

[10.46, 20.79]

20. Rounding Dictionary Values

prices = {
    "Laptop": 79999.987,
    "Mouse": 1499.456,
    "Keyboard": 2999.789
}

result = {
    product: round(price, 2)
    for product, price in prices.items()
}

print(result)

Output:

{'Laptop': 79999.99, 'Mouse': 1499.46, 'Keyboard': 2999.79}

21. Rounding List of Dictionaries

This is useful when working with API or database data.

products = [
    {"name": "Laptop", "price": 79999.987},
    {"name": "Mouse", "price": 1499.456},
    {"name": "Keyboard", "price": 2999.789}
]

for product in products:
    product["price"] = round(
        product["price"],
        2
    )

print(products)

Output:

[
    {'name': 'Laptop', 'price': 79999.99},
    {'name': 'Mouse', 'price': 1499.46},
    {'name': 'Keyboard', 'price': 2999.79}
]

22. Important: round() Uses Python's Rounding Rule

There is an important behavior with .5 values.

print(round(2.5))
print(round(3.5))

Output:

2
4

Python uses round half to even for exact halfway cases.

Another example:

print(round(4.5))
print(round(5.5))

Output:

4
6

The rule helps reduce systematic rounding bias when many values are rounded.

23. Rounding to Tens

You can use a negative ndigits value.

number = 1234

print(round(number, -1))

Output:

1230

Here -1 means rounding to the nearest 10.

24. Rounding to Hundreds

number = 1234

print(round(number, -2))

Output:

1200

25. Rounding to Thousands

number = 1234

print(round(number, -3))

Output:

1000

The pattern is:

round(number,  2) → 2 decimal places
round(number,  1) → 1 decimal place
round(number,  0) → nearest whole position
round(number, -1) → nearest 10
round(number, -2) → nearest 100
round(number, -3) → nearest 1000

26. Practical Example : Student Percentage

obtained_marks = 438 total_marks = 500

percentage = (
    obtained_marks / total_marks
) * 100

percentage = round(
    percentage,
    2
)

print("Percentage:", percentage)

Output:

Percentage: 87.6

27. Practical Example : Data Analysis

Suppose we calculate an average from several values.

sales = [
    12500.75,
    15200.45,
    13450.80,
    17800.35
]

average = sum(sales) / len(sales)

print(
    "Average Sales:",
    round(average, 2)
)

Output:

Average Sales: 14738.09

28. Practical Example : Machine Learning Prediction

Predictions can contain many decimal places.

predictions = [
    85.67891,
    92.45672,
    78.98761,
    88.12345
]

result = [
    round(prediction, 2)
    for prediction in predictions
]

print(result)

Output:

[85.68, 92.46, 78.99, 88.12]

29. round() vs int()

These functions behave differently.

number = 10.9

print(round(number))
print(int(number))

Output:

11
10

round() rounds the number, while int() removes the decimal portion.

round(10.9) → 11
int(10.9)   → 10

30. round() vs math.floor() and math.ceil()

Python's math module provides other ways to handle decimals.

import math

number = 10.7

print(round(number))
print(math.floor(number))
print(math.ceil(number))

Output:

11
10
11

The meanings are:

round()     → Round to nearest value
floor()     → Always go downward
ceil()      → Always go upward

31. Important Points to Remember

Basic usage

round(10.567)

Two decimal places

round(10.567, 2)

One decimal place

round(10.567, 1)

Nearest 10

round(1234, -1)

Nearest 100

round(1234, -2)

With map()

list(
    map(
        lambda x: round(x, 2),
        numbers
    )
)

With average

round(
    sum(numbers) / len(numbers),
    2
)

Easy way to remember

round(number)
        ↓
Nearest whole number

round(number, 2)
        ↓
Keep 2 decimal places

round(number, -1)
        ↓
Nearest 10
Interactive Sandbox
Python