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
moneyprecisiondecimalcorrectness
Use a decimal type for monetary calculations, not floating point
Accumulate and compare money with a Decimal/BigDecimal type rather than native floats to avoid rounding drift, and stay consistent with the rest of the codebase's money handling.
PR: hegnar-user-ws · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | let balance = 0; |
| 2 | for (const tx of txs) { |
| 3 | balance += isInflow(tx) ? Number(tx.amount) : -Number(tx.amount); |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | let balance = new Decimal(0); |
| 2 | for (const tx of txs) { |
| 3 | balance = isInflow(tx) ? balance.plus(tx.amount) : balance.minus(tx.amount); |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)