Cambridge pseudocode

How to Round in Pseudocode

ROUND(n, places) looks at the following digit and decides whether the last kept digit moves. places is how many digits survive after the decimal point. ROUND(3.14159, 2) keeps two, sees that the next digit is 1, and stays at 3.14.

How it works

A positive half steps up. INT never steps.

The interesting case is a tie. ROUND(2.5, 0) is 3: the .5 is enough to leave 2. INT does not make that decision. It deletes the fraction, toward zero, so INT(7.9) is 7 and INT(-2.3) is -2. Use ROUND when the question says “to 2 decimal places.” Use INT when it says “the whole number part.”

OUTPUT ROUND(3.14159, 2)
OUTPUT ROUND(2.5, 0)
OUTPUT INT(7.9)
OUTPUT INT(-2.3)
Output
3.14
3
7
-2

In Python

A half goes to the even integer

Python’s built-in round uses bankers’ rounding. A value ending in exactly 5 rounds to the nearest even digit, so round(2.5) is 2 and round(3.5) is 4. The 3.14 case matches Cambridge. The tie does not. The converter ships its own ROUND helper rather than calling Python’s round.

Python
print(round(3.14159, 2))  # 3.14
print(round(2.5))         # 2
print(round(3.5))         # 4
print(int(7.9))           # 7, same cut as INT

Beyond the exam

Why a half is a fight

If every positive .5 rounds up, a long column of measurements drifts high. Bankers’ rounding sends half of those ties up and half down, so the drift cancels. This compiler, like a school calculator on a positive number, takes the simpler rule: 2.5 becomes 3. Either rule is a choice about ties. They agree whenever the following digit is not exactly 5.

Run the example in the pseudocode compiler online, or read the matching section of the Cambridge pseudocode guide.