货币值的最佳SQL数据类型是什么?我正在使用MySQL,但更喜欢数据库独立的类型。
当前回答
For accounting applications it's very common to store the values as integers (some even go so far as to say it's the only way). To get an idea, take the amount of the transactions (let's suppose $100.23) and multiple by 100, 1000, 10000, etc. to get the accuracy you need. So if you only need to store cents and can safely round up or down, just multiply by 100. In my example, that would make 10023 as the integer to store. You'll save space in the database and comparing two integers is much easier than comparing two floats. My $0.02.
其他回答
Decimal(19,4)在大多数情况下工作得很好。您可以调整比例和精度,以适应您需要存储的数字的需要。即使在SQL Server中,我也不倾向于使用“money”,因为它是不标准的。
简单的回答:我建议使用十进制,精度根据您的需要。如果你想要存储整数的货币小单位(例如美分),并且在你的编程语言中处理小数有困难,可以选择精度= 0的Decimal。
为了找到所需的精度,你需要考虑以下几点:
您支持的货币类型(它们可以有不同数量的小数)。加密货币最多有18个小数(ETH)。由于通货膨胀,小数的数量可以随着时间的推移而改变。 存储小单位商品的价格(可能是从另一种货币转换的结果)或使用累加器可能需要使用比一种货币定义的更多的小数
如果您需要更改精度,则存储最小单元的整数可能导致将来需要重新缩放值。如果用小数,就简单多了。
文章中有更多细节和注意事项。
For accounting applications it's very common to store the values as integers (some even go so far as to say it's the only way). To get an idea, take the amount of the transactions (let's suppose $100.23) and multiple by 100, 1000, 10000, etc. to get the accuracy you need. So if you only need to store cents and can safely round up or down, just multiply by 100. In my example, that would make 10023 as the integer to store. You'll save space in the database and comparing two integers is much easier than comparing two floats. My $0.02.
虽然这可能有点晚,但对其他人来说是有帮助的。根据我的经验和研究,我已经知道并接受十进制(19,6),这是在使用php和mysql时。当工作与大量的钱和汇率
非常晚进入,但GAAP是一个很好的经验法则。对货币字段应用DECIMAL(13,4)就足够了。
如果你的应用程序需要处理高达一万亿的货币价值,那么这应该工作:13,2如果你需要遵守GAAP(公认会计原则),那么使用:13,4 通常你应该把你的货币值加到13,4,然后把输出四舍五入到13,2。
在MySQL中,您可以使用DECIMAL或NUMERIC数据类型,因为它们存储精确的数值数据值。
使用13,4可以得到999,999,999.9999美元。根据ISO 4217,只有2个国家使用小数点后4位(智利和乌拉圭)。
来源:
ISO 4217 -定义用于表示货币的alpha和数字代码。 流通货币一览表 在MySQL中存储货币值的最佳数据类型
推荐文章
- 如何在SQL中选择表的最后一条记录?
- SQL在Oracle中连接多行列值的查询
- 在单个查询中计算空值和非空值
- 在存储过程中使用“SET XACT_ABORT ON”有什么好处?
- 如何通过查询在MySQL中获得数据库结构?
- SQL to LINQ工具
- 如何从一个查询插入多行使用雄辩/流利
- 如何连接列在Postgres选择?
- MySQL删除表中的所有行,并将ID重置为零
- 在准备语句中使用“like”通配符
- MySQL中的表名是否区分大小写?
- 库未加载:libmysqlclient.16。在OS X 10.6上使用mysql2 gem运行'rails server'时出现dylib错误
- 有人可以对SQL查询进行版权保护吗?
- 如何知道MySQL表最近一次更新?
- 如何转储一些SQLite3表的数据?