Data & Integrity

Money & Currency Handling

Intermediate

We move and account for money, so the arithmetic must be exactly right. A fraction of a penny in the wrong type, rounding at the wrong moment, or two currencies added together can turn into real financial errors and hard reconciliation problems. Money has rules. Follow them precisely.

The first rule, and the one most often broken by people new to financial code: never use floating point (double or float) for money. Binary floating point cannot represent values like 0.10 exactly, so amounts drift. Use a decimal type, store the currency next to the amount, and decide on purpose when and how rounding happens.

Money is really three things that travel together: an amount, a currency, and a scale (how many decimal places). Treat them as one unit. Never mix currencies silently. Make rounding an explicit, documented choice, not an accident of the type system.

Represent money correctly

Float money double total = 0.1 + 0.2; // 0.30000000000000004
if (total == 0.3) { ... } // never true

Floating point cannot represent these values exactly, so totals drift and comparisons fail. Over many transactions this becomes real money lost and accounts that will not reconcile.

Decimal, explicit rounding decimal total = 0.1m + 0.2m; // exactly 0.3
var charged = Math.Round(total, 2, MidpointRounding.ToEven);

Decimal is exact, and rounding to the charged scale is one explicit, documented step.

Calculate and round deliberately

Self-review checklist

Why it matters: In a financial business, money bugs are not cosmetic. They cause real losses, failed reconciliations, unhappy customers, and regulatory questions. Exact decimal types, currency-aware amounts, and deliberate rounding are the difference between books that balance and a slow leak nobody can trace.