Saving Decimal Amounts with a Currency and Scale
A monetary value needs a currency, a scale, and a rounding contract. A fixed-scale decimal or lowest-unit integer can serve different domains.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a unit boundary
Value 125 as integer cents represents 1.25 in an appropriate currency. The amount 125 in a whole-unit column has a different meaning. Decimal representation describes storage and distribution implications.
Validate amounts when writing
For supported currencies, agree which precision the database keeps and verify one fractional and one boundary value. Column and domain rules can enforce supported ranges.[1][2]
Keep amount, currency and scale together
With a scale of two decimal places, storing 12.30 as integer 1230 minor units preserves the value exactly. Adding 12.30 of one currency to 5.00 of another requires a conversion or a grouped report. Currency and rounding policy belong in the model. See decimal input and money.
amount_minor = 1230
currency = example unit with 2 decimal places
Display = 12.30 in that currency
A different scale can give 1230 a different interpretation
Check the boundary where input exceeds permitted precision
A data model can reject too many fractional digits or apply a stated rounding rule. Test negatives, zero and the supported scale. Keep conversion rates and effective time where the domain requires reproducibility.