Derive order values, band them, and catch the rounding trap

short · 40 min · Objective 2.3

Task

Calculate each valid order's net value, bin it into bands, and total it two ways -- rounding each order first, and rounding only the total -- to see how far apart "the same" number can drift.

Steps

  1. For each order with a positive quantity, calculate net value as quantity times unit price times (1 minus discount_pct / 100), using data/orders.csv and data/products.csv.
  2. Band each value as small (under 50), medium (50 to 200) or large (over 200), and save the counts to lab/derive/bands.csv with header band,orders.
  3. Record in lab/derive/rounding.txt the lines rounded first: N (each value rounded to whole pounds, then summed) and rounded last: N (the exact sum, rounded to whole pounds).

Verify

These checks run in a POSIX shell with Python 3: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. On Windows, python3 is often a Microsoft Store placeholder that prints “Python was not found”; run alias python3=py in Git Bash first.

awk -F, 'NR>1 {print $1": "$2; n+=$2} END {print n" orders banded"}' lab/derive/bands.csv
grep -E '^rounded (first|last): ' lab/derive/rounding.txt
awk -F': ' '/first/ {f=$2} /last/ {l=$2} END {print "rounding first adds "f-l}' lab/derive/rounding.txt

434 small, 1054 medium and 509 large orders, 1997 in all. Rounding first gives 354719 and rounding last 354627: rounding first adds 92, a total that no longer matches the data it came from. Round for display, never before summing.

This is an independent study companion for CompTIA Data+ DA0-002 and is not produced by or endorsed by CompTIA.