Storing money as INTEGER
Store cents, not dollars - the one money recipe that works everywhere.
- Family
- Exact decimals
- Missing in
- None of the five
- Gotchas
- 4 listed
Engine by engine
BIGINT (or NUMBER(20)) cents are exact in all five engines including SQLite, at the cost of handling currency minor units yourself.
| Moteur | Type | Stockage | Notes |
|---|---|---|---|
| SQL Server |
BIGINT
|
8 bytes | Cents fit easily: BIGINT spans about +/-92 quadrillion dollars in cents. |
| PostgreSQL |
BIGINT
|
8 bytes | Same recipe; pair with a view or generated column for dollar formatting. |
| MySQL |
BIGINT
|
8 bytes | SUM() over BIGINT returns DECIMAL, so totals stay exact even past the 64-bit range. |
| SQLite |
INTEGER
|
up to 8 bytes | SQLite's native integer is the natural home for cents - there is no decimal type to prefer. |
| Oracle |
NUMBER(20)
|
1-12 bytes | A 20-digit integer NUMBER dwarfs any realistic cent total. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- Never store money in FLOAT or DOUBLE: 0.1 + 0.2 drifts. INTEGER cents and DECIMAL are the only safe homes.
- Minor units differ per currency: JPY has no cents and BHD has three decimals, so a hardcoded division by 100 breaks both.
- Sums of cents can overflow the application's integers (exact only to 2^53 in JavaScript) even when they fit in BIGINT. SQL Server's SUM over BIGINT returns BIGINT and raises an overflow error past 2^63; PostgreSQL and MySQL return NUMERIC/DECIMAL.
- Round at the boundaries only: store exact cents and apply tax or proration rounding once per line, in one documented policy.
Common questions
Why INTEGER over DECIMAL?
It behaves identically in all five engines including SQLite, sums exactly, and forces a single rounding policy into application code.
What about currencies without cents?
Define the minor unit per currency - 1 for JPY, 1000 for BHD - and store that unit. The column is unit-agnostic; the formatter is not.
Related on SQLly
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.