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

jramnani
Hello, I am almost done going through the book, Programming Phoenix 1.4, again after a hiatus from using Elixir for a few years. It see...
New
dimitarvp
Hello, While coding tests for a hobby project of mine, I stumbled upon something odd when directly comparing Decimals – which is a bad i...
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
halostatue
I wanted to share an exercise I did that I found useful for refining my knowledge of SQL internal implementations and list comprehensions...
New
wolfiton
Cool way to learn flexbox and have fun at the same time. https://mastery.games/p/flexbox-zombies
New
eksperimental
Let’s share Elixir documentation &amp; learning material written-in/translated-to languages other than English such as docs, books, cours...
New
sonic182
Hi everyone, I’d like to share Livellm, a small open-source Phoenix LiveView chat app built as a reference for integrating llm_composer ...
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
sergio
https://www.udemy.com/course/interviewing-tips-and-tricks-for-remote-engineers/ I always wanted to create my own course and share things...
New
crockwave
I updated my fork of the live_view_hooks demo repo to include fullCalendar integration, including JSON API, event creation and storage in...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement