Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing currency values in SQLite3

I'm dealing with lots of different currencies in my application, and I want to know what the "best" way is to store them in an SQLite3 database.

I'm leaning towards a fixed-point representation (i.e. store them as integers, where $3.59 gets stored as 359, ¥400 stored as 40000). Is this a good idea? What if my input data later changes and requires more precision?

like image 772
nornagon Avatar asked Jul 16 '10 01:07

nornagon


2 Answers

Given that SQLite 3 will use up to 8 bytes to store INTEGER types, unless you are going to have numbers greater than 10^16, you should be just fine.

To put this in perspective, the world gross domestic product expressed in thousandths of a USD (a mill) is about 61'000'000'000'000'000 which sqlite3 has no problem expressing.

sqlite> create table gdp (planet string, mills integer);
sqlite> insert into gdp (planet, mills) values ('earth', 61000000000000000000);
sqlite> select * from gdp;
earth|61000000000000000000

Unless you are handling interplanetary accounting, I don't think you have to worry.

like image 130
msw Avatar answered Oct 06 '22 20:10

msw


In financial software currency is always represented as fixed-point (decimal). You can emulate it in SQLite using integers (64-bit integer holds up to 18 digits).

like image 39
Piotr Avatar answered Oct 06 '22 20:10

Piotr