joaquinalcerro

joaquinalcerro

Multi-currency applications - What is a acceptable approach?

Hi everyone.

I am currently starting a refactor of a small application that only handles two currencies but I do not really like how I implemented it. Basically what I did was pretty naive… I just added a boolean currency type and another field with conversion rates. This led to some trouble when user tried to use documents of different currencies for math operations.

I also save the amounts in normal formats (example: 000,000.00) but I have seen some posts suggesting using cents.

On the other side of things, one a document starts its lifecycle with a currency, everything related to it must be in the same currency with out the user having to specify the currency.

So anyone suggest a better approach for handling multi-currency?

Best regards.

Most Liked

kip

kip

ex_cldr Core Team

You might want to read this post regarding money libraries (I am the author of one of them).

A couple of basic thoughts on money handling:

  • Unless you are completely in control of the currencies in use and you are very familiar with different currency rules (number of decimal places for example - currencies have any of 0, 1, 2, 3 or four places) use Decimal. Using integers means you still need to track decimal places unless you can guarantee all currencies you are using use the same decimal digits.

  • Always tag the money amount with its currency. A tagged tuple like {:AUD, 1234} is fine, or a struct (which is what libraries usually use) and only ever do operations on the structure. Over time you’ll want to trace how money amounts are transformed and create some guarantees - for example you want to guarantee you don’t add two money amounts together in different currencies.

  • Use ISO4217 currency codes so that you can interchange with other apps and users easily and have no confusion on what is meant. Tag your document with the ISO4217 code per your described use case.

  • Always do money calculations at maximum precision and round only when you need a final result. This is how financial apps work, typically at a precision of at least 7 digits. Another reason to use Decimal is to allow arbitrary arithmetic calculations.

  • Use Banker’s rounding which in the Decimal library is :half_even.

  • Be aware that different currencies have rounding rules different for cash amounts than accounting amounts. For example, the Australia dollar cash amount rounds to the nearest 5c whereas the accounting amount rounds to the cent.

  • If you need to serialize money amounts (for example to a database), I recommend using a composite type in Postgres so that the currency is always associated with the amount.

14
Post #3
dimitarvp

dimitarvp

I suspect @kip might have a few things to say about all that… Check his libraries, by the way.

olivermt

olivermt

I just started implementing ex_money and I feel like the authors are holding my hand and spoon feeding me how to implement an app that works with money :joy:

I think its one of the best libs I’ve had the pleasure of using for a domain I didn’t know well going into it.

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
mmport80
I have put far too much effort into Dialyzer over the last year or so - and basically - I doubt it’s worth the effort. It’s not as easy ...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
tmbb
This is a post to discuss the new Phoenix LiveView functionality. From Chris’s talk, it appears that they generate all HTML on the serve...
342 18272 126
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19675 166
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement