Skip to main content

How to Store Money in SQLite as Whole Cents

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
CREATE TABLE <table> (<cents column> INTEGER NOT NULL);

SELECT printf('%.2f', <cents> / 100.0) FROM <table>;
PartWhat it means
<cents column> INTEGERHolds each amount as a whole number of cents: 10 for $0.10
<cents> / 100.0Turns 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 --version to 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:

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;

The money_in_cents.sql script in a macOS-style code window: a cart table with a REAL price and an INTEGER price_cents, a pen at 0.10 or 10 cents and an eraser at 0.20 or 20 cents, a SELECT of sum(price) and sum(price_cents), and a SELECT that prints the cents total as dollars with printf.

  • price REAL stores each price as a decimal, the way it's written.
  • price_cents INTEGER stores 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, and AS total names the result.

Step 2: Run It​

In the folder that holds the script, run:

Terminal
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.

Output
+---------------------+------------------+
| sum(price) | sum(price_cents) |
+---------------------+------------------+
| 0.30000000000000004 | 30 |
+---------------------+------------------+
+-------+
| total |
+-------+
| 0.30 |
+-------+

The output in a terminal: sum(price) is 0.30000000000000004 while sum(price_cents) is 30, then the total printed as 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​

MistakeWhat happensDo this instead
Dividing by 100 instead of 100.01999 / 100 gave 19: dividing one whole number by another drops the decimalsDivide 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 1Compare whole cents

Troubleshooting​

SymptomLikely causeFix
A total shows digits like 0.30000000000000004The amounts are stored as REALStore whole cents in an INTEGER column
Parse error near line 1 of money_in_cents.sql: table cart already existsThe script already ran on cart.dbDelete 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​