Common sense when storing currencies?

After you read correctly about how to best handle users in multiple time zones , I found out that the way to save all dates in a normalized time zone application is UTC , and then apply the difference between the normalized time zone and the time zone of individual users when conclusion. Today I came to the conclusion that it would be advisable to apply this approach to currency processing in software:

All saved currency is converted into the common currency of the application, say EUR (€), and when withdrawing, the currency is converted back to the user's own currency with the updated daily exchange rate?

What common sense is here? How is this generally solved and what should I know before deciding how to handle this?

+4
source share
2 answers

One standard approach is to store both the amount and the currency when monetary values ​​are held and processed.

See “Money Template” in Martin Fowler’s Enterprise Application Architecture Templates.

Fowler describes the definition of a simple data type for storing two primitive components with overloaded arithmetic operators for performing monetary operations:

"The main idea is to have a Money class with fields for a numeric amount and currency. You can save the sum as an integral type or a fixed decimal type. Decimal type is easier for some manipulations, integral for others. You should absolutely avoid any type of floating type points, as this will lead to a kind of rounding problem that money should avoid.Most of the time people want monetary values ​​to be rounded to the smallest full unit, such as cents in dollars. However, there are times when there are fractional . Unit needs is important to find out how much money you are working, especially in an application that uses both kinds makes sense to have different types for the two cases. They behave very differently at arithmetic.

Money needs arithmetic so that you can use money objects as easily as you use numbers. But arithmetic operations for money are some important differences from monetary operations in numbers. Most obviously, any addition or subtraction should be possible if you try to add money of different currencies together. The simplest and most common answer is to view the addition of disparate currencies as a mistake. Still in difficult situations, you can use the idea of ​​Ward Cunninghamas about a money bag. This is an object containing the sums of several currencies together in one object. Then this object can participate in settlements, like any monetary object. It can also be valued in currency. "

+3
source

The difference between processing time and currency is that time zones do not change.

When processing monetary values, you must consider what the currency of actual money is. If the actual money is in US dollars and you store it as EUR, you will get a discrepancy between the actual value and the stored value when shifting their values.

Alternatively, you will have to recalculate all values ​​when updating the exchange rate, but this will remove the goal of storing values ​​in one currency.

+1
source

All Articles