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:

  1. Takes a number as input from the user (float).
  2. Takes the number of decimal places to round to (integer).
  3. Outputs the rounded number.

Cheat sheet

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

round(number, ndigits)
  • number: The value to round
  • ndigits: Decimal places to keep (optional)
print(round(4.567))     # 5
print(round(4.567, 2))  # 4.57
print(round(456.78, -1))  # 460

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

print(round(2.5))  # 2 (rounds down — 2 is even)
print(round(3.5))  # 4 (rounds up — 4 is even)

Try it yourself

# Write code here
quiz iconTest yourself

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

All lessons in Logic & Flow