CharlesO
It would be nice to have a built in datatype for handling money values.
Most currency activities are exactly two decimal places.
Such a built-in convenience would be more helpful that requiring the decimal library, for example since all we need is 100.00 or 2-decimal place accuracy at most.
Please what suggestions do you have? or should we just be using floats for this, since the required precision is exactly 2 decimal places.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Qqwy
[money] (money | Hex) does exactly what you want. It is not part of the standard library because it does not need to be. This is actually a great advantage because now it can be updated independently.
CharlesO
Enum.reduce(data, 0, &sum.(&1, &2))I’m pulling data from a database and cannot know ahead of time if all my data would be Money types, but i just need to perform very simple “math” on the data that pull from the db
The code sample above fails when using Decimal: ERR:
%ArithmeticError{message: "bad argument in arithmetic expression"}Qqwy
In that case I’d advise you to use Numbers, which is made to solve exactly that problem.
Do note that
Moneydoes not followNumbers’ specification completely right now, though (And adding that would probably be a backwards-incompatible change).Another approach would be to define a summation protocol and implement this for any datatypes you might want to use.
tmbb
This package is probably better, and defines money datatypes for databases (currently postgresql and mysql, I think):
kip
According to the data I have there are:
But you should also consider that some currencies have specific rounding rules for cash values that relate to the physical currency in circulation.
For example, the AUD and CHF have cash rounding to 0.05 but normal rounding to 0.01.
Additionally there is the question as to when to round and to what precision. In a chain of financial transactions it is most common (according to my research, not an formal statement) that rounding is performed only at the end of the chain.
Then if you are splitting up money (like allocating it to various amounts) then rounding has to be handled carefully to ensure that any remainder is taken care of.
Anyway, money handling and its implication in the real world is an interesting problem - but its not just about decimal arithmetic (I am the author of ex_money and suggestions and PRs are always welcome)
CharlesO
thanks for the stats, but see where the trouble lies … I feel Elixir should make handling decimals natural, so we can do stuff like 100.00 + 10 … and it should just work
OvermindDL1
Using a protocol, say via @Qqwy’s number library, you could easily just redefine
+to call it’s protocol dispatchedaddfunction, then it would just work with, say,Decimal.Something like
100.00 + 10is ill defined though, I hate hate hate it when languages allow it. Do you want it to be110.00or do you want it to be110or do you want it to come out as some new type or etc… etc… That is why I like statically strongly typed languages making both operands to+be the same type, that way it is properly formed.kip
From a decimal and money point of view, how would you want it to manage
100.00 / 3? The issues of rounding, when to round, what kind of rounding all still need to be managed and the defaults are probably not what you want and expect.Rounding is often done in the “half up” fashion - as it is for floats in Elixir/erlang, for financial transactions you probably want to use “half even”.
If you do
(100.00 / 3) * 3.2351then when should the rounding be applied? At each step or only at the end? Where would you specify the rounding in such an expression?I’m not sure a language can apply the necessary convenience and yet be transparent on the underlying mechanisms. And given that Elixir favours being explicit over implicit I suspect this isn’t a likely focus for the core team.
CharlesO
Understood. But for day to day usage, how best should we handle 2-decimal exact precision numbers? If I’m working with money, then I know that 100.00 +10 is ALWAYS 110.00 - I have a context, I’m working with MONEY
tmbb
You should convert everything to money before adding. The question of 100.00 + 10 shouldn’t even come up