Python Reference Library →

Python round() Function – Round Numbers to Nearest Integer or Decimal Places

Let’s look at round() function – round numbers to nearest integer or decimal places

  1. Notice

    round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 2.5
print(round(number))
Live state

Top-level scope

number→Object 1
Object 1 · float2.5
This step

round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties.

Picture round() function – round numbers to nearest integer or decimal places

round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties.

Image brief

Alt text
A diagram for Python round() Function – Round Numbers to Nearest Integer or Decimal Places. Instructions: number = 2.5 print(round(number)). Displayed result: 2. The final example bindings are number --> object 1 (float). object 1: 2.5
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python round() Function – Round Numbers to Nearest Integer or Decimal Places". Teaching focus: round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 2.5 print(round(number)) | v DISPLAYED RESULT 2 FINAL BINDINGS number --> object 1 (float) object 1: 2.5 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python round() Function – Round Numbers to Nearest Integer or Decimal Places. Instructions: number = 2.5 print(round(number)). Displayed result: 2. The final example bindings are number --> object 1 (float). object 1: 2.5

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 2.5
print(round(number))
Live state

Top-level scope

number→Object 1
Object 1 · float2.5
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
2

What do you expect this program to print?

What does this example display?

number = 2.5
print(round(number))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

2

Follow the printed values in order. The output is: 2

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties.

round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties.

More examples and reference

Python round() Function

The round() function in Python is used to round a number to the nearest integer or to a specified number of decimal places. It’s simple, effective, and very commonly used in mathematical and financial calculations.

Syntax

round(number, ndigits)

Parameters:

  • number – The number to be rounded (int or float).
  • ndigits (optional) – The number of decimal places to round to. If omitted, it rounds to the nearest integer.

Returns:

  • The rounded number (int if ndigits is not given; float if ndigits is provided).

Example 1: Round to Nearest Integer

print(round(5.7))
6

Example 2: Round Down

print(round(5.4))
5

Example 3: Round to Decimal Places

print(round(3.14159, 2))
3.14

Use Case: Financial Rounding

amount = 123.4567
rounded_amount = round(amount, 2)
print("Final bill amount:", rounded_amount)
Final bill amount: 123.46

Rounding Even Numbers (Bankers’ Rounding)

Python uses “round half to even” also known as “bankers’ rounding.”

print(round(2.5))  # Rounds to even number
print(round(3.5))  # Also rounds to even number
2
4

Common Mistakes

  • Passing a string instead of a number → raises TypeError
  • Expecting math.ceil or floor behavior – use math.ceil() or math.floor() for those cases
  • Assuming round always rounds up – it depends on the value and follows "round half to even"

Interview Tip

The round() function is commonly used in data formatting and financial calculations. Be ready to explain how rounding works, especially the even-number rounding logic.

Summary

  • round(x) → rounds to nearest integer
  • round(x, n) → rounds to n decimal places
  • Follows “round half to even” strategy (not always up)

Practice Problem

Ask the user to enter a number and the number of decimal places. Print the rounded result.

num = float(input("Enter a number: "))
decimals = int(input("Round to how many decimal places? "))
print("Rounded result:", round(num, decimals))

What to remember

Connect what you noticed in the example with the idea below.

Remember

round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties.

Watch out for this idea

round() always rounds exact halfway values away from zero.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →