将货币价值作为美分/次要单位存储的缺点是什么? [英] What are the downsides to storing money values as cents / minor units?

查看:294
本文介绍了将货币价值作为美分/次要单位存储的缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,一些金融api就像信用卡处理的条码api需要以美分的形式传递,这似乎是一个很好的简化,它让我想知道为什么我在我的应用程序中到处都不一样我目前在我的Java代码中使用Postgres和BigDecimal的数据库NUMERIC无限长度,但是我以简单的方式存储钱作为分。

I have noticed that some financial api's like stripe api for credit card processing require that amounts be passed in as cents, this seems like a good simplification and it is making me wonder why I don't do the same everywhere in my application I am currently using database NUMERIC unlimited length with Postgres and BigDecimal in my Java code but I am tempted by the simplicity of storing money as cents.


  1. 将钱存为分钱有什么缺点?

  2. 是否有具体的操作难以用钱作为美分?

  3. 存款为美分的货币是否与所有的全球货币一起使用,如果
    另有特殊情况,则会对不同货币造成
    的巨大混乱?

  4. 有没有一个很好的java库来处理
    钱作为美分?

  1. What are the downsides of storing money as cents.
  2. Are there specific operations that are hard to do with money as cents?
  3. Does money stored as cents work with all world wide currencies or does accounting for different currencies become a gigantic mess of if else special cases?
  4. Is there a good java library for working with money as cents?

更新对于没有美分的货币,我们可以将问题概括为将货币存入货币的最小小单位,因此美元和其他货币相当于美元。 p>

UPDATE For currencies that don't have cents, we can generalize the question to storing money as the smallest minor unit for a currency, so cents for dollars and whatever the equivalent is for other currencies.

推荐答案

缺点是你不能代表分数单位。如果您执行兴趣计算等,这是一个问题。但是,如果你正在做一个真正的钱交易,这个问题就消失了。

The downside is that you can't represent fractional units. This is a problem if you are performing interest calculations or the like. However, if you are making a "real money" transaction, this issue goes away.

如果货币是非十进制,这是无关紧要的 - 只要有一个最小的单位,你可以用它代表任何其他的金额。您可能需要更聪明地了解如何以可读的格式显示,如果这是相关的,但这是一个单独的问题。

It doesn't matter if the currency is non-decimal - as long as there is a smallest unit, you can use that to represent any other amount. You may need to be a little more clever in how you display in human readable format, if that is relevant, but that's a separate issue.

事实上,使用最小的单位可能会简化执行转换,因为您可以计算每个货币对的最小单位之间的转换率。当然,一旦转化率,你最终将需要再次使用BigDecimal(等等)。

In fact, using the smallest unit is likely to simplify performing conversions, as you can work out the conversion rate between the smallest unit of each currency pair. Of course, once you're into conversion rates, you'll end up needing to use BigDecimal (or the like) again.

总之,可能最好使用最小单位作为您的面额,并使用BigDecimal处理小数部分(如果可能出现)。 不要使用IEEE浮点数,因为它们不能正确地表示所有的十进制数字。这将导致会令某人失望的种种错误。

So, in summary, it may well be best to use both smallest unit as your denomination, AND to use BigDecimal to handle fractional parts if they can arise. Don't use IEEE floats, because they can't represent all decimal numbers correctly. This will lead to the kinds of errors that will upset someone.

这篇关于将货币价值作为美分/次要单位存储的缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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