储存货币的常识?
在阅读了如何正确正确处理多个时区的用户后,我了解到,可行的方法是将所有日期存储在标准化的、应用程序范围的时区 - UTC
中,然后在输出时应用标准化时区和单个用户时区之间的差异。今天我开始思考在软件中应用这种方法处理货币是否合适:
所有存储的货币都转换为应用程序范围的货币,比如说EUR
(欧元),并且在输出时,货币被转换回用户自己的货币,并使用当天的更新汇率?
这里有什么常识?通常如何解决此问题?在选择处理此问题的方法之前我应该注意什么?
After reading up on how to best handle users in multiple timezones properly, I've learned that the way to go is to store all dates in an normalized, application-wide timezone - UTC
and then apply the diff between the normalized timezone and the individual users timezone when outputting. Today I came to think if this would be appropriate to apply this approach to handling currency in software:
All stored currency are converted to a application-wide currency, lets say EUR
(€), and when outputting, the currency is converted back into the users own currency, with an updated exchange rate of the day?
What's common sense here? How is this generally solved and what should I be aware of before choosing a way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种标准方法是在持有和操纵货币价值时同时存储金额和货币。
请参阅 Martin Fowler 的《企业应用程序架构模式》中的货币模式。
Fowler 描述了定义一个简单的数据类型来保存两个原始组件,并使用重载的算术运算符来执行货币运算:
One standard approach is to store both an amount and a currency whenever monetary values are held and manipulated.
See the Money Pattern in Martin Fowler's Patterns of Enterprise Application Architecture.
Fowler describes defining a simple datatype to hold the two primitive components, with overloaded arithmetical operators for performing monetary operations:
处理时间和货币之间的区别在于,时区的价值不会改变。
在处理货币价值时,您必须考虑实际货币是什么货币。如果实际货币为美元,而您将其存储为欧元,则当实际价值与存储价值发生变化时,您将发现实际价值与存储价值之间存在差异。
或者,您必须在汇率更新时重新计算所有值,但这会消除以单一货币存储值的目的。
The difference between handling time and currency, is that time zones doesn't shift in value.
When handling monetary values you have to consider what the currency of the actual money is. If the actual money is in USD and you store it as EUR, you will get a discrepancy between the actual value and the stored value when their values shift.
Alternatively you would have to recalculate all values when the exchange rate is updated, but that would take away the purpose of storing the values in a single currency.