Menu
Coddy logo textTech

Round Numbers

Part of the Logic & Flow section of Coddy's Python journey — lesson 5 of 78.

The round() function rounds numbers to the nearest value.

round(number, ndigits)
  • number: The value to round.
  • ndigits: Decimal places to keep (optional).

Examples:

print(round(4.567))     # 5
print(round(4.567, 2))  # 4.57
print(round(456.78, -1))  # 460.0

Python rounds halfway cases to the nearest even number (banker's rounding):

print(round(2.5))  # 2  (rounds DOWN to even 2)
print(round(3.5))  # 4  (rounds UP to even 4)
challenge icon

Challenge

Easy
Write a program that takes two separate inputs from the user and outputs the rounded result:

1. The first input should be a number (float). 2. The second input should be the number of decimal places to round to (integer).

Example:
If the inputs are 3.14159 and 2, the program should output 3.14.

Try it yourself

# Take a float input from the user
# Take an integer input from the user for the number of decimal places
# Print the rounded number
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow