All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Numeric data: rounding, truncation and random numbers

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Handling numbers means choosing how to round, how to cut off decimals and how to generate random values.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Rounding (spec 8.2.1)

round(x, n) rounds to n decimal places; round(x) gives an int. Values that are not exactly halfway round to the nearest value, as you would expect.

print(round(3.14159, 2))
print(round(7.6), round(-7.6))
print(round(1234, -2))

Output:

3.14
8 -8
1200

Halfway values: what the PLS says and what Python does

The Programming Language Subset describes round() as using 'the 0.5 rule'. Python 3 itself does not always round a half up: an exact half goes to the nearest even value (round half to even, sometimes called banker's rounding). Also, many decimals such as 2.675 are stored as binary floats slightly below or above the value you typed, so they may round the 'wrong' way.

print(round(2.5), round(3.5), round(-2.5))
print(round(0.125, 2), round(2.675, 2))

Output:

2 4 -2
0.12 2.67

The code you write in the exam runs in real Python, so expect this behaviour. If a task needs halves always rounded up for positive values, use math.floor(x + 0.5) instead, and test the halfway cases in your IDE.

Truncation, floor and ceiling

Truncation cuts off the decimal part (towards zero). floor rounds down, ceil rounds up. They differ for negative numbers.

import math
print(int(8.9), math.trunc(8.9))
print(math.floor(-2.3), math.trunc(-2.3), math.ceil(-2.3))
print(math.ceil(7 / 3))
print(math.sqrt(49), math.pow(2, 3), math.fabs(-4))

Output:

8 8
-3 -2 -2
3
7.0 8.0 4.0

math.ceil is useful for 'how many boxes are needed' problems.

Random numbers

The random library gives:

import random
dice = random.randint(1, 6)
cards = [1, 2, 3, 4]
random.shuffle(cards)
percent = random.random() * 100

To fill a list with 100 random integers from 0 to 100, as in the sample paper, append random.randint(0, 100) inside a loop of 100 passes.

Data type conversions

int(), float() and str() convert between numeric and text forms; remember / always gives a float, so convert with int() if a whole number is required (or use //).

Worked example

Each minibus carries 16 people. How many minibuses are needed for 75 people, and how many spare seats are there?

  1. 75 / 16 = 4.6875, so round up: math.ceil(75 / 16) = 5
  2. Spare seats = 5 x 16 - 75 = 5

Answer: 5 minibuses, 5 spare seats

Key idea: round(x, n) rounds (halves go to even); int() and math.trunc() cut towards zero; math.floor() rounds down and math.ceil() up; random.randint(a, b) includes both ends.

Check yourself

  1. What is displayed when this code runs? ```python print(round(12.3456, 1)) ```
    Show answer

    12.3. To 1 decimal place 12.3456 is 12.3.

  2. What is displayed when this code runs? ```python import math print(math.trunc(-6.7)) ```
    Show answer

    -6. Truncation removes the decimal part, moving towards zero: -6.

  3. What is displayed when this code runs? ```python import math print(math.floor(-6.2)) ```
    Show answer

    -7. floor rounds down to the next integer below: -7.

  4. What is displayed when this code runs? ```python import math print(math.ceil(41 / 10)) ```
    Show answer

    5. 41 / 10 = 4.1, and ceil rounds up to 5.

  5. What is displayed when this code runs? ```python print(round(4.5), round(5.5)) ```
    • 5 6
    • 4 5
    • 4 6
    • 5 5
    Show answer

    4 6. Python 3 rounds exact halves to the even neighbour, whatever the PLS's '0.5 rule' wording suggests: 4 6.

  6. Which values can random.randint(1, 6) return?
    • Any integer from 1 to 6 inclusive
    • Any integer from 1 to 5
    • Any integer from 0 to 6
    • Any real number between 1 and 6
    Show answer

    Any integer from 1 to 6 inclusive. randint includes both end points, unlike range().

  7. This code should simulate rolling two dice and print their total, which must be between 2 and 12. Which line completes it? ```python import random ok = True for trial in range(2000): __________ if total < 2 or total > 12: ok = False print(ok) ```
    • `total = random.randint(0, 6) + random.randint(0, 6)`
    • `total = random.randint(1, 12)`
    • `total = random.randint(1, 6) + random.randint(1, 6)`
    • `total = random.randint(2, 13)`
    Show answer

    `total = random.randint(1, 6) + random.randint(1, 6)`. Each die gives 1 to 6, so the total is 2 to 12. randint(1, 12) stays in range but can give 1, so over 2000 trials it fails the check.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.