dimitarvp

dimitarvp

Decimal comparison oddity: sharing how I solved it

Hello,
While coding tests for a hobby project of mine, I stumbled upon something odd when directly comparing Decimals – which is a bad idea but it’s inevitable when doing assert equal in tests with maps that contain Decimals.

iex> {:ok, d1} = Decimal.parse("00020.400")
{:ok, #Decimal<20.400>}
iex> {:ok, d2} = Decimal.parse("00020.4")
{:ok, #Decimal<20.4>}
iex> Decimal.cmp(d1, d2)
:eq
iex> > d1 == d2
false

The last part made my tests fail. What further surprised me is that there is a way to equalize Decimals that have to parse such zero-padded data: Decimal.reduce.

iex> {:ok, d1} = Decimal.parse("00020.400"); d1 = Decimal.reduce(d1)
{:ok, #Decimal<20.4>}
iex> {:ok, d1} = Decimal.parse("00020.4"); d2 = Decimal.reduce(d2)
{:ok, #Decimal<20.4>}
iex> d1 == d2
true

Now the tests succeed.

Maybe this will help you one day. :024:

(Alternatively, you could just do String.trim(your_string, "0") before parsing, which will rid you of all zeroes both in front and at the back.)

Most Liked

LostKobrakai

LostKobrakai

==/2 does structural compasions and not logical. Therefore you shouldn‘t use it for structs, where different values in the structs‘ fields are still considered the same value, which is true for decimals (different number ot trailing 0‘s) but also for some core structs like datetimes. Use functions of those structs to compare them instead of the equals operator.

Where Next?

Popular in Other Resources Top

zkessin
I have released a free email course on how to build releases in Elixir with Distillery, The course is text based. You can subscribe here...
New
ErlangSolutions
We’ve announced our latest webinar with Fred Hebert entitled ‘Testing Erlang and Elixir through PropEr Modelling’. There are a ton of tec...
New
mkunikow
Microsoft: DEVOPS200.9x Architecting Distributed Cloud Applications Note: This course is not related to Elixir at all, but I found it ver...
New
Timtonix
A tous les français de la communauté Elixir, J’ai découvert hier ce livre traitant d’élixir Elixir - Un langage de programmation 100 %...
New
realcorvus
This document is to help you prevent a data breach due to your Elixir web application being hacked. It covers strategic and technical wor...
New
ryan-senn
I’m learning Elixir and started to go through the Project Euler problems. While the problems are great, the UX is very limited. I’ve buil...
New
theodore
Hello, Like many people on Earth, I’ve been trying to understand the transformer architecture. I translated the Python code in https://w...
New
tmartin8080
I was having issues understanding how to deploy Phoenix applications, and decided to write an article on how I was eventually able to get...
New
djaouen
Has anyone seen this paper on static typing for Elixir? It kind of goes over my head, but I’m interested in what the community thinks ab...
New
GenericJam
It seems like most employers want a reasonably high degree of competence before they’re willing to look at a candidate. Perhaps it’s beca...
New

Other popular topics Top

Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
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

We're in Beta

About us Mission Statement