Data Cleaning Exercises

Module 6 · Numbers and money · 8 min

Exact decimals for money

You'll be able to: Parse amounts into exact decimals, reject text that would need rounding, and compare thresholds precisely.

The idea

Floating point can't represent 0.1 exactly, so sums drift. Money belongs in a decimal type with a declared scale: DECIMAL(12,2) in SQL, decimal.Decimal in Python.

Casting text to DECIMAL can round silently: DuckDB turns "1.234" into 1.23. If the rules say amounts have at most two decimals, validate the text first and reject the rest.

Thresholds are exact too: "greater than 5000.00" means 5000.00 itself is not flagged.

Worked example

Input: amount: "12.5", "-15.25", "5000.00"

Visual
Convert amount to decimal(12,2) → Flag needs_review where amount > 5000.00
SQL (DuckDB)
CAST(amount AS DECIMAL(12,2)) > 5000 AS needs_review
pandas
Decimal(s).quantize(Decimal("0.01")) > Decimal("5000.00")

Result: 12.50 false, -15.25 false, 5000.00 false

Common mistakes

  • Floats for money.
  • Silent rounding on cast.
  • Using >= when the rule says greater than.

Check yourself

Is 5000.00 greater than 5000.00?

No, so it isn't flagged.

What does DuckDB do with CAST('1.234' AS DECIMAL(12,2))?

Rounds to 1.23, which is why you validate first.

Practice it

Flag sales above 5000.00 and keep refunds.

Next: Flag outliers, don't delete them