在SQLite3中存储货币值 [英] Storing currency values in SQLite3

查看:258
本文介绍了在SQLite3中存储货币值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理应用程序中的许多不同货币,我想知道将它们存储在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.

我倾向于定点表示(即,将它们存储为整数,其中$ 3.59被存储为359,¥400被存储为40000)。这是一个好主意吗?

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?

推荐答案

鉴于SQLite 3将最多使用8个字节来存储< a href = http://www.sqlite.org/datatype3.html rel = noreferrer> INTEGER 类型,除非您要使用大于10 ^ 16的数字,否则就可以了。

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.

从角度看,以千分之一美元(工厂)表示的世界国内生产总值约为61'000'000'000'000'000 sqlite3表示没有问题。

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.

这篇关于在SQLite3中存储货币值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆