Storing currency values in SQLite3

允我心安 提交于 2019-12-18 03:17:48

问题


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?


回答1:


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.




回答2:


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




回答3:


I'd say string/text. You can always convert it to whatever culture if you need to do that as well.




回答4:


best currency DataType is BigDecimal and in sqlite you can store it as a VARCHAR field

ormlite convert BigDecimal to string when you store it.

ormlite doc for BigDecimal




回答5:


SQLite has no BigDecimal field So I use the SQLite Integer type and convert like so: BigDecimal bd = new BigDecimal("1234.5678"); int packedInt = bd.scaleByPowerOfTen(4).intValue(); // packedInt now = 12345678 Now save packedInt to SQLite Integer field. To go back to BigDecimal: BigDecimal bd = new BigDecimal(packedInt); // bd = 12345678 bd = bd.scaleByPowerOfTen(-4); // now bd = 1234.5678



来源:https://stackoverflow.com/questions/3261220/storing-currency-values-in-sqlite3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!