← ACARA Digital Technologies Years 9–10

Testing decimal results with round()

Floats can be slightly inexact (0.1 + 0.2 is not 0.3), so round the actual result to a set number of decimal places before comparing it with the expected value.

Understanding Testing decimal results with round() in ACARA Digital Technologies Years 9–10

What it does: round(..., 2) matches the 2 decimal places of money, so tiny float errors cannot cause a false FAIL. 0.1 + 0.2 is stored as 0.30000000000000004, so a direct == comparison with 0.3 fails. round(0.30000000000000004, 2) gives 0.3, which equals the expected value. round() accepts any number of places. Pick the number that suits the problem, such as 2 for dollars and cents. Round only the actual result, then compare it using ==. Putting == inside round() rounds a True/False value. Rounding is needed for float results, which can be slightly inexact. Whole-number ints are exact. The first result has a tiny float error. The second is exact, so it passes either way.

Common mistakes: The test is meant to check 0.1 + 0.2 against 0.3. Multiplying gives 0.02, so the check would fail for the wrong reason. It should be act = 0.1 + 0.2. A single = is assignment, not comparison, so this is a syntax error. Use == to compare: ok = round(total, 2) == 25.0.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 591–622) and word search (Modules 536–622).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
In Python, what does 0.1 + 0.2 == 0.3 give?
False, because the float result is slightly inexactTrue, because 0.1 + 0.2 is exactly 0.3
Matches
print(0.1 + 0.2 == 0.3)
↓
Comparing a float result directly with == gives False because 0.1 + 0.2 is 0.30000000000000004. Fix: print(round(0.1 + 0.2, 2) == 0.3)
Applying
Meanings
Term
round(actual, 2) == expected
Meaning
Floats can be slightly inexact (0.1 + 0.2 is not 0.3), so round the actual result to a set number of decimal places before comparing it with the expected value.
Practice
actual = 0.1 + 0.2
Testing
Gaps
print((0.1 + 0.2, 2) == 0.3)
Check Answer
Spots
1exp = 0.3
2act = 0.1 * 0.2

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Reindeer' badge — Dictionary Guide, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. In Python, what does 0.1 + 0.2 == 0.3 give?
A. True, because 0.1 + 0.2 is exactly 0.3
B. False, because the float result is slightly inexact ✓
0.1 + 0.2 is stored as 0.30000000000000004, so a direct == comparison with 0.3 fails.
2. Which test is the better way to check a decimal result against an expected value?
A. round(actual, 2) == expected ✓
B. actual == expected
Rounding the actual result first removes tiny float errors, so the test compares fairly.
3. A test expects 0.3 but the code returns 0.30000000000000004. The test uses round(actual, 2) == 0.3. What is the verdict?
A. PASS, because round gives 0.3 ✓
B. FAIL, because the tiny extra digits are still compared
round(0.30000000000000004, 2) gives 0.3, which equals the expected value.
4. Which line correctly tests a price total of 19.99 + 5.01 that should be 25.0?
A. round(total) == 25.0 and ignore the cents
B. round(total, 2) == 25.0 ✓
Money needs 2 decimal places. Rounding to a whole number would hide errors in the cents.
5. Why do we choose the number of decimal places in round(actual, 2)?
A. To match the precision the expected value needs, such as cents for money ✓
B. Because round() only works with exactly 2 decimal places
round() accepts any number of places. Pick the number that suits the problem, such as 2 for dollars and cents.

Match the Pairs

round(actual, 2) == expected → Round the actual result to 2 decimal places, then compare it with the expected value to test a decimal result
0.1 + 0.2 == 0.3 → Evaluates to False in Python because the float sum is really 0.30000000000000004
A test for a price calculation rounds the actual result to 2 decimal places (cents) before comparing it with the expected value → print(round(price * 3, 2) == 7.5)
Choose the number of decimal places to match the precision of the expected value, such as 1 decimal place for an expected 0.3 → round(0.1 + 0.2, 1) == 0.3
print(0.1 + 0.2 == 0.3) → Comparing a float result directly with == gives False because 0.1 + 0.2 is 0.30000000000000004. Fix: print(round(0.1 + 0.2, 2) == 0.3)
round(actual, 2) == 0.333333 → The expected value has more decimal places than the rounded result, so they never match and the test fails. Fix: round(actual, 2) == 0.33

Key Term

round(actual, 2) == expected
Floats can be slightly inexact (0.1 + 0.2 is not 0.3), so round the actual result to a set number of decimal places before comparing it with the expected value.

Fill in the Blanks

1. print(round(0.1 + 0.2, 2) == 0.3)
Hint: Type the built-in function name that trims a float to a set number of decimal places.
2. actual = 0.1 + 0.2 print(round(actual, 2) == 0.3)
Hint: Type the number of decimal places to keep when rounding the result.
3. actual = 0.1 + 0.2 print(round(actual, 2) == 0.3)
Hint: Type the operator that checks whether the rounded result is equal to the expected value.
4. expected = 0.3 if round(0.1 + 0.2, 2) == expected: print("PASS")
Hint: Type the variable that holds the value the test should produce.
5. actual = 0.1 * 3 expected = 0.3 print(round(actual, 2) == expected)
Hint: Type the variable holding the calculated result that needs rounding before the comparison.

Spot the Error

Question 1
A exp = 0.3
B act = 0.1 * 0.2
C print(round(act, 2) == exp)
Line B — The test is meant to check 0.1 + 0.2 against 0.3. Multiplying gives 0.02, so the check would fail for the wrong reason. It should be act = 0.1 + 0.2.
Question 2
A total = 19.99 + 5.01
B ok = round(total, 2) = 25.0
C print(ok)
Line B — A single = is assignment, not comparison, so this is a syntax error. Use == to compare: ok = round(total, 2) == 25.0.
Question 3
A p = 0.1 / 3
B exp = 0.3
C print(round(p, 2) == exp)
Line A — The expected value 0.3 comes from 0.1 * 3. Dividing gives about 0.033, so the test would fail. It should be p = 0.1 * 3.
Question 4
A actual = 1.1 + 2.2
B if actual == 3.3:
C print('PASS')
D else:
E print('FAIL')
Line B — 1.1 + 2.2 is stored as 3.3000000000000003, so comparing directly with == gives False. Round first: if round(actual, 2) == 3.3:.
Question 5
A expected = 0.3
B actual = 0.1 + 0.2
C r = round(actual, 2)
D if r != expected:
E print('PASS')
F else: print('FAIL')
Line D — != means 'not equal', so PASS would print when the values differ, which reverses the test. It should be if r == expected:.

Student with a class code? Join your class →