Data Cleaning Exercises

Module 6 · Numbers and money · 6 min

Rounding on purpose

You'll be able to: Round only where a rule says so, with a declared mode, and never silently during casting.

Before this: Exact decimals for money

The idea

Casting '1.234' to DECIMAL(12,2) rounds to 1.23 without warning. If the rules say amounts must have at most two decimals, reject 1.234 instead.

When rounding is required, name the mode (half-even, half-up) and round once, at the end.

Worked example

Input: amount: '1.234'

Visual
Convert type → decimal(12,2) after a format check
SQL (DuckDB)
CASE WHEN amount ~ '^-?\d+(\.\d{1,2})?$' THEN CAST(amount AS DECIMAL(12,2)) END
pandas
s.str.fullmatch(r"-?\d+(\.\d{1,2})?")

Result: 1.234 → exception, not 1.23

Common mistakes

  • Silent rounding.
  • Rounding intermediate results.

Check yourself

Why check the format before casting?

Because the cast may round silently.

Practice it

Flag large transactions, keep refunds.

Next: Flag large transactions, keep refunds