Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
number = 2.5
print(round(number))Top-level scope
2.5round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
number = 2.5
print(round(number))
|
v
DISPLAYED RESULT
2
FINAL BINDINGS
number --> object 1 (float)
object 1: 2.5Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
number = 2.5
print(round(number))Top-level scope
2.5The example has finished. Compare the final output with the values and references above.
2
What do you expect this program to print?
number = 2.5
print(round(number))round() FunctionThe 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.
round(number, ndigits)
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.ndigits is not given; float if ndigits is provided).print(round(5.7))
6
print(round(5.4))
5
print(round(3.14159, 2))
3.14
amount = 123.4567
rounded_amount = round(amount, 2)
print("Final bill amount:", rounded_amount)
Final bill amount: 123.46
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
TypeErrormath.ceil() or math.floor() for those casesThe round() function is commonly used in data formatting and financial calculations. Be ready to explain how rounding works, especially the even-number rounding logic.
round(x) → rounds to nearest integerround(x, n) → rounds to n decimal placesAsk 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))
Connect what you noticed in the example with the idea below.
round() rounds to the requested precision. Exact halfway cases use ties to even; binary floating-point can affect apparent decimal ties.
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.