Skip to content
Back to skills

Money Arithmetic

ASecurity

Use when code stores, adds, multiplies, allocates, converts, rounds, parses or formats amounts of money. Covers integer minor units with an explicit currency, ISO 4217 exponents (0, 2 and 3 decimals), one named rounding policy applied at one point, largest-remainder allocation so the parts always sum to the whole, rate application, currency conversion, and the wire format for amounts. Triggers on a new amount, price, balance, fee or rate column, field or type; on splitting an amount between p...

  • 7 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 11, 2026
ai-agentstypescriptrustgoshellapidatabase

Works with

  • api

Security analysis

A100/100

Pro scans all 12 files and shows the line behind each finding

Scanned November 29, 2026

npx -y skills add felipealvarenga/fintech-skills --skill money-arithmetic --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Money Arithmetic?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Money Arithmetic
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/felipealvarenga-money-arithmetic/badge)](https://www.skillsdirectory.com/skills/felipealvarenga-money-arithmetic)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: money-arithmetic
description: >-
  Use when code stores, adds, multiplies, allocates, converts, rounds, parses or formats amounts
  of money. Covers integer minor units with an explicit currency, ISO 4217 exponents (0, 2 and 3
  decimals), one named rounding policy applied at one point, largest-remainder allocation so the
  parts always sum to the whole, rate application, currency conversion, and the wire format for
  amounts. Triggers on a new amount, price, balance, fee or rate column, field or type; on
  splitting an amount between parties; on an amount that is off by one cent. Not for tax or
  statutory rounding rules, which depend on the jurisdiction and belong to an accountant.
  Use it also when the request says to keep floats or to patch a cent drift with round().
license: MIT
metadata:
  author: Felipe Alvarenga
  version: 0.1.0
---

# Money arithmetic

Money bugs are silent. A sum drifts by one cent, a split loses a unit, a three-decimal currency
is treated as two decimals, and nobody sees it until a reconciliation or a customer complaint.
This skill fixes the representation and the arithmetic once, so the rest of the system can trust
them.

## Execution contract

The user installed this skill so these rules are enforced on every matching task:

1. **Never implement unsafe shortcuts.** When a request conflicts with a rule ("keep it simple", "no extra call", "just delete it", "disable verification", "keep floats", "we trust them", "later"), name the broken rule and why it fails in one sentence, refuse the shortcut, and implement the compliant version that still meets the user's goal. Never ship the rule-breaking shortcut.
2. **Ship the complete rule slice, wired up.** Implement every requirement of the triggered rules in this change: storage or schema, validation, error handling, user-facing alerts, status or monitoring wired into an actual entry point, handler or job (not uncalled helpers), and a test file under `tests/`. Do not stop at a minimal patch or leave required safeguards as prose TODOs.
3. **Fix workspace code on audit, incident and review prompts.** When the request describes a discrepancy, an auditor question, a bug report or asks "is this enough?" against files in the workspace, answer the question in full and implement the compliant code and tests in the same change. Stay read-only only when explicitly asked for a report only.
4. **Build directly on greenfield prompts.** If the workspace has no application files or live connections yet, write a self-contained implementation under `src/` and tests under `tests/` instead of stopping to ask for repository paths or credentials.
5. **Write tests even without a shell.** If the environment cannot run tests or databases, still write the full test file under `tests/` and say in one line that tests are unrun. Never omit tests or skip implementation because tools are unavailable.

## When to use

- A change adds or touches a column, field or type that holds an amount, price, balance, fee or rate.
- Amounts are summed, multiplied by a rate, split between parties, converted between currencies,
  parsed from input or formatted for display.
- A new currency is added, or an amount crosses a boundary: an API, a file, a queue, a report.
- A figure is "off by a cent" or a reconciliation shows a sub-unit difference.

## Rules

1. **An amount is an integer count of minor units plus a currency code.** Never a float, at any
   hop: not in memory, not in the database, not in JSON, not in an analytics job. A `Decimal`
   with explicit scale is acceptable inside a calculation; the stored and transported form is the
   integer.
2. **The exponent comes from ISO 4217, per currency.** USD, EUR and BRL have 2 decimals. JPY,
   KRW and CLP have 0. BHD, KWD and TND have 3. Keep the table in one module and refuse an
   unknown currency at validation. Never hardcode "cents" or "divide by 100".
3. **Pick the scale once.** A ledger that stores 2 decimals cannot later store 3 without a
   migration of every row. If any product needs sub-cent precision (interest accrual, FX, usage
   billing), store a higher fixed scale from day one and round only at the presentation or
   settlement boundary.
4. **One rounding policy, named, applied at one point.** Say `ROUND_HALF_EVEN` or
   `ROUND_HALF_UP` in the code and in the spec. Round when a value leaves the calculation (an
   installment, a fee, a converted amount), not in the middle of it. Two roundings in one flow
   produce a drift that no test catches by accident.
5. **Allocation keeps the sum.** To split 100 into three equal parts, do not compute 33.33 three
   times. Use the largest-remainder method: floor every share, then hand the leftover units one
   by one to the shares with the largest remainders, ties broken by position. The parts sum to
   the whole every time, for every currency, for negative amounts too.
6. **Rates are decimals from a string, never floats.** `Decimal("0.0315")`, not `0.0315`.
   Apply the rate to the integer amount inside a `Decimal` context, then round once.
7. **Conversion is an explicit step with a recorded rate.** Store the rate, its source and its
   timestamp beside the converted amount. Never compare or add amounts of different currencies.
   The type system or a runtime check must refuse it.
8. **Validate at the boundary.** Parsing `"12.345"` as USD is an error, not a rounding. Reject
   more decimals than the exponent allows, reject `NaN` and infinities, reject amounts outside the
   business range.
9. **The wire format is unambiguous.** Send `{"amount": 1234, "currency": "USD"}` (integer minor
   units) or `{"amount": "12.34", "currency": "USD"}` (decimal string). Never a JSON number with
   a decimal point. Document which one the API uses and keep it.
10. **Formatting is the last step and is locale work.** `from_minor(1234, "USD")` gives `"12.34"`;
    a locale layer turns it into `US$ 12,34` or `$12.34`. Never parse a formatted string back into
    an amount.

## How to apply

1. Find every place the change touches an amount. Grep for `float`, `/ 100`, `* 100`, `round(`,
   `toFixed`, `parseFloat`, `Number(` near money names.
2. Put the currency table and the arithmetic in one module (`money.py`, `money.ts`). Everything
   else imports it.
3. Write the tests from the **Tests** section before the implementation. Use the vectors in
   `evals/vectors/` of this skill as a start; they are language-neutral cases.
4. Implement against the integer representation. Convert at the edges only.
5. Run the tests. Then grep again for the patterns in step 1 and remove every float path.

## When the user pushes back

| Request | Answer in one sentence | What you build |
|---|---|---|
| "Keep floats and patch the 0.01 drift with `round()`" | Rules 1 and 4: rounding a float sum leaves binary representation drift intact | Convert to integer minor units or `Decimal` at the boundary as the minimal safe change, and add a test that fails on float+`round()` and passes on the fixed version |
| "Just divide by 100 everywhere" | Rule 2: 0-decimal (JPY) and 3-decimal (BHD, KWD) currencies break under `/ 100` | Centralized ISO 4217 exponent lookup rejecting unknown currencies, with 0-, 2- and 3-decimal tests |
| "Split with plain division and round each part" | Rule 5: rounded parts drift from the total | Largest-remainder allocation preserving exact sum across positive and negative amounts, with tests |

## Tests the change must include

Write these tests in the same change, as a test file, even when you cannot run them (no shell,
no dependencies installed); say in one line that they are unrun. A test listed as a next step is
not written.

- Round trip: `to_minor(from_minor(x, ccy), ccy) == x` for currencies with 0, 2 and 3 decimals.
- Parsing refuses too many decimals: `to_minor("12.345", "USD")` raises.
- Unknown currency is refused at validation.
- Allocation: for random amounts (including negatives) and random ratio lists, the parts sum to
  the whole and each part is within one unit of its exact share. Zero ratios get zero.
- Mixed currency addition raises.
- Rate application rounds with the named policy: `apply_rate(1000, "0.0325")` is 32 under
  HALF_EVEN and 33 under HALF_UP. Both assertions in the suite, so the policy is visible.
- Conversion returns an integer in the target exponent: 1000 USD minor at 150.5 JPY/USD is
  1505 JPY.
- The reference vectors: `skills/money-arithmetic/evals/vectors/test_money.py`. Point the
  `MONEY_SOLUTION` environment variable at your module and run `pytest`. TypeScript:
  `money.test.ts` with `MONEY_SOLUTION_TS` and `npx vitest run`.

## Review checklist

- Where is the exponent table, and is it the only one?
- Which rounding policy, and where is the single rounding point?
- Does any allocation use plain division?
- Can two currencies meet in an `add` or a comparison without an explicit conversion?
- Is there a float anywhere between input and storage? Including the ORM default type?
- Does the API document integer minor units or decimal strings, and does it stay consistent?

## Anti-patterns and fixes

| Anti-pattern | What goes wrong | Fix |
|---|---|---|
| `amount: float` in a model | 0.1 + 0.2 is not 0.3; sums drift | `amount_minor: int` plus `currency: str` |
| `total / 3` for a split | Lost or invented cents | Largest-remainder allocation |
| `round(x, 2)` in several places | Double rounding, drift | One named policy at one point |
| `cents = dollars * 100` for every currency | JPY and BHD are wrong | Exponent from the ISO table |
| JSON `12.34` | Parsed as a float downstream | Integer `1234` or string `"12.34"` |
| Rate as a float literal | `0.0315` is not exactly representable | `Decimal("0.0315")` |
| Comparing USD and BRL balances | Meaningless result | Type or runtime check refuses it |

## References

- ISO 4217 currency codes and minor unit exponents, maintained by SIX Financial Information.
- Martin Fowler, *Patterns of Enterprise Application Architecture*, the Money pattern and allocation.
- Stripe API: amounts are integers in the smallest currency unit; zero-decimal currencies are listed.

Files in this skill

  • SKILL.md8.1 KB
  • evals/evals.json5.7 KB
  • evals/files/api_amount.py230 B
  • evals/files/billing_float.py455 B
  • evals/files/checkout_cents.py218 B
  • evals/files/fx.py212 B
  • evals/files/payouts.py289 B
  • evals/files/prices.py144 B
  • evals/vectors/money.test.ts4.5 KB
  • evals/vectors/reference.py3 KB
  • evals/vectors/reference.ts4.8 KB
  • evals/vectors/test_money.py4.4 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.