储存货币的常识?

发布于 2024-12-07 06:39:32 字数 379 浏览 5 评论 0原文

在阅读了如何正确正确处理多个时区的用户后,我了解到,可行的方法是将所有日期存储在标准化的、应用程序范围的时区 - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潦草背影 2024-12-14 06:39:32

一种标准方法是在持有和操纵货币价值时同时存储金额和货币。

请参阅 Martin Fowler 的《企业应用程序架构模式》中的货币模式

Fowler 描述了定义一个简单的数据类型来保存两个原始组件,并使用重载的算术运算符来执行货币运算:

“基本思想是拥有一个带有数字字段的 Money 类
金额和货币。您可以将金额存储为
整数类型或固定小数类型。小数类型更容易
一些操作,另一些的积分。你绝对应该
避免任何类型的浮点类型,因为这会引入
Money 旨在避免的舍入问题。大多数时候
人们希望货币价值四舍五入到最小的完整单位,
例如美元中的美分。然而,也有小数的时候
需要单位。弄清楚钱的种类很重要
您正在使用的应用程序,尤其是在使用两者的应用程序中
种。对于这两种情况有不同的类型是有意义的
它们在算术下的表现完全不同。

货币需要算术运算,以便您可以将货币对象用作
就像使用数字一样容易。但是金钱的算术运算有
货币运作在数字上有一些重要的区别。最多
显然,任何加法或减法都需要了解货币,因此
如果您尝试将不同的钱加在一起,您可以做出反应
货币。最简单、也是最常见的应对措施是治疗
将不同货币加在一起作为错误。在更多的一些
在复杂的情况下,你可以使用沃德·坎宁安(Ward Cunningham)的金钱观
包。这是一个包含多种货币的对象
一起在一个物体中。然后这个对象可以参与
就像任何货币对象一样进行计算。它也可以被评估为
货币。”

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 basic idea is to have a Money class with fields for the numeric
amount and the currency. You can store the amount as either an
integral type or a fixed decimal type. The decimal type is easier for
some manipulations, the integral for others. You should absolutely
avoid any kind of floating point type, as that will introduce the kind
of rounding problems that Money is intended to avoid. Most of the time
people want monetary values rounded to the smallest complete unit,
such as cents in the dollar. However, there are times when fractional
units are needed. It’s important to make it clear what kind of money
you’re working with, especially in an application that uses both
kinds. It makes sense to have different types for the two cases as
they behave quite differently under arithmetic.

Money needs arithmetic operations so that you can use money objects as
easily as you use numbers. But arithmetic operations for money have
some important differences to money operations in numbers. Most
obviously, any addition or subtraction needs to be currency aware so
you can react if you try to add together monies of different
currencies. The simplest, and most common, response is to treat the
adding together of disparate currencies as an error. In some more
sophisticated situations you can use Ward Cunningham’s idea of a money
bag. This is an object that contains monies of multiple currencies
together in one object. This object can then participate in
calculations just like any money object. It can also be valued into a
currency."

寂寞清仓 2024-12-14 06:39:32

处理时间和货币之间的区别在于,时区的价值不会改变。

在处理货币价值时,您必须考虑实际货币是什么货币。如果实际货币为美元,而您将其存储为欧元,则当实际价值与存储价值发生变化时,您将发现实际价值与存储价值之间存在差异。

或者,您必须在汇率更新时重新计算所有值,但这会消除以单一货币存储值的目的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文