• Home
  • 3.10 Rounding Numbers

3.10 Rounding Numbers

View Categories

3.10 Rounding Numbers

2 min read

Rounding is the process of reducing the number of digits in a number while keeping its value as close as possible to the original.

Python provides the built-in round() function to round numbers.

Rounding is commonly used when displaying measurements, prices, percentages, averages, and other values where excessive decimal places are unnecessary.


Syntax #

round(number)

or

round(number, digits)

Parameters #

Parameter Description
number The number to be rounded.
digits (Optional) Number of decimal places to keep. If omitted, Python rounds to the nearest integer.

Return Value #

Returns the rounded value.


Example 1: Rounding to the Nearest Integer #

main.py

number = 7.6

print(round(number))

Output #

8

Explanation #

  • 7.6 is closer to 8 than 7.
  • Therefore, round() returns 8.

Example 2: Rounding Down #

main.py

number = 7.3

print(round(number))

Output #

7

Explanation #

  • 7.3 is closer to 7.
  • Therefore, the rounded result is 7.

Example 3: Rounding to Two Decimal Places #

main.py

pi = 3.14159265

print(round(pi, 2))

Output #

3.14

Explanation #

  • The second argument specifies the number of decimal places.
  • The value is rounded to two digits after the decimal point.

Example 4: Rounding to Three Decimal Places #

main.py

value = 12.987654

print(round(value, 3))

Output #

12.988

Explanation #

  • The number is rounded to three decimal places.
  • The fourth decimal digit determines whether the third digit is increased.

Example 5: Rounding an Integer #

main.py

number = 125

print(round(number))

Output #

125

Explanation #

  • Integers do not have decimal places.
  • Therefore, the value remains unchanged.

Example 6: Rounding Negative Numbers #

main.py

number = -8.6

print(round(number))

Output #

-9

Explanation #

  • The round() function also works with negative numbers.
  • -8.6 is closer to -9 than to -8.

Example 7: Rounding User Input #

main.py

number = float(input("Enter a decimal number: "))

print(round(number, 1))

Sample Input #

45.678

Output #

45.7

Explanation #

  • float() converts the user input into a floating-point number.
  • round(number, 1) keeps one digit after the decimal point.

Example 8: Rounding in Calculations #

main.py

average = (82 + 91 + 76) / 3

print(round(average, 2))

Output #

83.0

Explanation #

  • The average is calculated first.
  • The result is then rounded to two decimal places.
  • Since the result already has one decimal place, no additional digits appear.

Example 9: Rounding to Tens #

main.py

number = 276

print(round(number, -1))

Output #

280

Explanation #

  • A negative value for the second argument rounds digits to the left of the decimal point.
  • -1 rounds to the nearest multiple of 10.

Example 10: Rounding to Hundreds #

main.py

number = 864

print(round(number, -2))

Output #

900

Explanation #

  • -2 rounds to the nearest hundred.
  • Since 864 is closer to 900 than to 800, the result is 900.

Common Mistakes #

1. Assuming round() Always Returns a Float #

Incorrect

print(type(round(8.6)))

Incorrect Expectation #

<class 'float'>

Actual Output #

<class 'int'>

Reason

When no second argument is provided, round() returns an integer if the result is an integer.


2. Forgetting the Parentheses #

Incorrect

print(round)

Output #

<built-in function round>

Reason

round is the function object itself.

Correct

print(round(5.7))

3. Passing a String Directly #

Incorrect

print(round("3.14"))

Output #

TypeError: type str doesn't define __round__ method

Reason

round() expects a numeric value.

Correct

print(round(float("3.14")))

4. Expecting round() to Modify the Variable #

Incorrect

number = 5.78

round(number)

print(number)

Output #

5.78

Reason

round() returns a new value.
It does not modify the original variable.

Correct

number = round(number)

print(number)

Best Practices #

  • Use round() when displaying values to users.
  • Specify the required number of decimal places instead of relying on the default behavior.
  • Store the returned value if it will be used later.
  • Convert user input to a numeric type before rounding.
  • Use negative values for the second argument when rounding to tens, hundreds, or thousands.

Key Points to Remember #

  • Python provides the built-in round() function for rounding numbers.
  • Without a second argument, round() rounds to the nearest integer.
  • The optional second argument specifies the number of decimal places.
  • Negative values for the second argument round digits to the left of the decimal point.
  • round() works with both integers and floating-point numbers.
  • round() returns a new value and does not modify the original variable.
  • Convert strings to numeric types before passing them to round().

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *