Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: JavaScript
moneyprecisioncorrectness
Never use floating-point numbers for money
Represent monetary amounts as integer minor units (cents) or a decimal type, and only format to a float for display. JS `number` is IEEE-754 binary floating point, so arithmetic like 0.1 + 0.2 drifts and rounding errors accumulate in totals.
PR: profico-org-corpus batch2 (all stacks/products)Created: Jul 26, 2026
Bad example
Old codets
| 1 | const total = 0.1 + 0.2; // 0.30000000000000004 |
| 2 | const withTax = price * 1.25; // accumulates rounding error |
Explanation (EN)
Binary floats can't represent most decimal fractions exactly, so sums and taxes drift.
Objašnjenje (HR)
Good example
New codets
| 1 | // store & compute in integer cents |
| 2 | const totalCents = 10 + 20; // 30 |
| 3 | const withTaxCents = Math.round(priceCents * 125 / 100); |
| 4 | const display = (withTaxCents / 100).toFixed(2); |
Explanation (EN)
Integer minor units keep arithmetic exact; convert to a decimal string only for display (or use a decimal library).
Objašnjenje (HR)