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
krishna_vaguelyright
Hi I am new to Elixir, just started learning 3 weeks back. I am building a Chat App with Elixir/Phoenix as backend &amp; React-Native on ...
New
ErlangSolutions
This month we’re excited to host Tyr Chen, VP of Engineering at ArcBlock, on our webinar. ArcBlock are founding sponsors of the Erlang E...
New
Morzaram
Hey guys, I went through Chris McCords Twitter clone video and made the code that shows the 1.6 version, along with doing a broadcast del...
New
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
luckywatcher
I’ve been working in Elixir for two years this month. I’ve enjoyed the community and the help I’ve received and want to give back in a sm...
New
hectorperez
Hi, I found that a great way to learn is: play with existing livebook notebooks create new notebooks — completely new or just changing ...
New
ErlangSolutions
If anyone is interested in joining our next webinar with Bruce Tate :backhand_index_pointing_right:registration is now open! RSVP and lea...
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
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

Other popular topics Top

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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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 42633 214
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement