Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
803 views
in Technique[技术] by (71.8m points)

sqlite - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...