How to Store Money in SQLite as Whole Cents
A price like 0.10 looks exact, but SQLite stores decimals as floating point numbers, which are approximate, and their sums can come out a hair off. Storing whole cents avoids that. This guide adds up two prices both ways, with real output from SQLite 3.54.1.
Syntax
CREATE TABLE <table> (<cents column> INTEGER NOT NULL);
SELECT printf('%.2f', <cents> / 100.0) FROM <table>;
| Part | What it means |
|---|---|
<cents column> INTEGER | Holds each amount as a whole number of cents: 10 for $0.10 |
<cents> / 100.0 | Turns cents back into dollars. The .0 makes it a division with decimals |
printf('%.2f', <number>) | Writes the number with exactly two decimal places |
SQLite's documentation on floating point numbers: "Floating point values are approximate."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as money_in_cents.sql:
CREATE TABLE cart (
item TEXT NOT NULL,
price REAL NOT NULL,
price_cents INTEGER NOT NULL
);
INSERT INTO cart (item, price, price_cents) VALUES ('Pen', 0.10, 10);
INSERT INTO cart (item, price, price_cents) VALUES ('Eraser', 0.20, 20);
SELECT sum(price), sum(price_cents) FROM cart;
SELECT printf('%.2f', sum(price_cents) / 100.0) AS total FROM cart;

price REALstores each price as a decimal, the way it's written.price_cents INTEGERstores the same price as a whole number of cents.sum()adds up a column.printf('%.2f', sum(price_cents) / 100.0)turns the cents total into dollars with two decimals, andAS totalnames the result.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table cart.db ".read money_in_cents.sql"
sqlite3 opens the database file cart.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
+---------------------+------------------+
| sum(price) | sum(price_cents) |
+---------------------+------------------+
| 0.30000000000000004 | 30 |
+---------------------+------------------+
+-------+
| total |
+-------+
| 0.30 |
+-------+

sum(price) came out as 0.30000000000000004, not 0.3: 0.10 and 0.20 can't be stored exactly as floating point numbers, and their tiny errors showed up in the sum. sum(price_cents) gave exactly 30, and dividing by 100.0 only at the end printed 0.30.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Dividing by 100 instead of 100.0 | 1999 / 100 gave 19: dividing one whole number by another drops the decimals | Divide by 100.0: 1999 / 100.0 gave 19.99 |
Comparing decimal sums with = | 0.1 + 0.2 = 0.3 gave 0, false, while 10 + 20 = 30 gave 1 | Compare whole cents |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A total shows digits like 0.30000000000000004 | The amounts are stored as REAL | Store whole cents in an INTEGER column |
Parse error near line 1 of money_in_cents.sql: table cart already exists | The script already ran on cart.db | Delete cart.db and run the script again |
FAQ
Which cent amounts can a REAL store exactly?
Only four. The documentation: "the only cents value that can be exactly represented are 0.00, 0.25, 0.50, and 0.75. Any other numbers to the right of the decimal point result in an approximation."
Is printf() the same as format()?
Yes. format('%.2f', 1999 / 100.0) returned 19.99, and printf('%.2f', 5 / 100.0) returned 0.05. The documentation: "The printf() SQL function is an alias for the format() SQL function."
Conclusion
Added up as REAL, 0.10 and 0.20 gave 0.30000000000000004. Stored as 10 and 20 cents, they gave exactly 30, which printf('%.2f', 30 / 100.0) printed as 0.30.
Additional Resources
- Floating Point Numbers — why decimals are approximate.
- format() — the format string and its options.
