{"success":true,"name":"never-do-money-math-in-floats-store-cents-as-integers-round-","description":"Handling prices, totals, discounts, or balances anywhere in the stack","category":"ai-brimind","path":"ai-brimind/never-do-money-math-in-floats-store-cents-as-integers-round-/SKILL.md","file":"SKILL.md","content":"---\nname: never-do-money-math-in-floats-store-cents-as-integers-round-\ndescription: \"Handling prices, totals, discounts, or balances anywhere in the stack\"\n---\n\n# Never Do Money Math in Floats: Store Cents as Integers, Round On Purpose, Name the Currency\n\n## When to use\nThe app touches money — prices, payments, balances, discounts, VAT — and amounts are (or are about to be) stored and computed as floating-point numbers like 19.99, with rounding happening wherever it happens.\n\n## Example request\n0.1 + 0.2 in JavaScript → 0.30000000000000004; floats cannot represent most decimal fractions exactly, so float money math leaks fractions of cents that eventually surface as a total that is off by one. Storing price as 19.99 and computing a 15% discount, then VAT → each step compounds tiny binary errors and two code paths round differently, producing invoices where the lines do not sum to the total; storing 1999 cents as an integer makes every addition exact. A balance compared with === after float arithmetic → 'insufficient funds' for a user whose balance is mathematically sufficient; integer cents make equality checks trustworthy again. Displaying 1999 to the user → the integer is for STORAGE and MATH; formatting to '19.99 TND' happens only at the display edge, like timezone conversion — same border, same discipline.\n\n## Instructions\nAct as a developer handling money with an accountant looking over your shoulder — one who knows that floating-point numbers are binary approximations that cannot exactly hold most decimal values, so every float operation on money is a tiny lie, and compounding tiny lies is how invoices stop adding up and audits begin. For any money-touching code, do this: (1) Store amounts as INTEGERS in the smallest currency unit — cents/millimes — in the database and in all computation: 19.99 becomes 1999. Integer addition and subtraction are exact, comparisons are trustworthy, and the entire class of 0.30000000000000004 bugs becomes unrepresentable. If the project already has float amounts in the DB, that is a data migration to plan deliberately, with the conversion rule written down. (2) Confine the decimal form to the two borders, exactly like your timezone discipline: parse user input ('19.99') into integer cents once at the entry, format integer cents into a localized display string once at the exit (using Intl.NumberFormat or the formatter the project already uses) — no float form living between the borders. (3) Make every division and percentage a CONSCIOUS rounding decision, because 1999 cents at 15% is 299.85 cents and something must decide: round where, in whose favor, at which step? Write the rule once ('round half up, at the line-item level, then sum') and enforce it through one shared money utility — two endpoints rounding differently is how the total mismatches its own lines. Multiply before dividing where possible to keep precision. (4) Name the currency explicitly next to every amount (field or constant), even in a single-currency app — an integer 'amount: 1999' without currency is a trap for the day a second currency or a payment provider (which will demand amounts in cents anyway — they all do, for exactly these reasons) enters the picture. (5) Verify with the accountant's test: generate an invoice with several lines, discounts, and tax, then check that the displayed lines sum EXACTLY to the displayed total — floats fail this test eventually, integers with one rounding rule pass it forever. Only mark money code done when no float ever carries an amount between input and display, and you can point at the single utility that owns the rounding rule — 'it looked right in my test' is not the bar, because float errors are engineered to look right until the one combination where they don't.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #226\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}