Terbium-135

Terbium-135

Ex_money: casting JSON to Money is not respecting decimal point

Why do I see different behavior here?

While doing the cast in IEX I see the expected result:

iex [17:06 :: 22] > %{"amount" => "1.5", "currency" => "EUR"} |> Money.Ecto.Composite.Type.cast()
{:ok, Money.new(:EUR, "1.5")}

the application is somehow respecting the locale setting (which is “de” in the case and “.” is not a comma delimiter but “,” is):

  Map.get(params, "balance") #=> "{\"currency\":\"EUR\",\"amount\":\"1.00\"}"
|> Jason.decode!() #=> %{"amount" => "1.00", "currency" => "EUR"}
|> Money.Ecto.Composite.Type.cast() #=> {:ok, Money.new(:EUR, "100")}

Thus getting back a Money value multiplied by 100

Marked As Solved

mudasobwa

mudasobwa

Creator of Cure

One might set the default locale in custom Cldr module as

defmodule MyApp.Cldr do
  use Cldr, locales: ["en", "fr", "de"], default_locale: "en"
end

or explicitly pass the locale in a call to cast/2 (no idea why it’s not documented, a PR documenting it would be appreciated, I believe.)

iex|🌢|1 ▶ %{"amount" => "1.00", "currency" => "EUR"}
          |> Money.Ecto.Composite.Type.cast(locale: :de)
{:ok, Money.new(:EUR, "100")}
iex|🌢|2 ▶ %{"amount" => "1.00", "currency" => "EUR"}
          |> Money.Ecto.Composite.Type.cast(locale: :en)
{:ok, Money.new(:EUR, "1.00")}

Also Liked

kip

kip

ex_cldr Core Team

Typically I don’t document behaviour implementations, but I agree that in this case its important to understand the behaviour (no pun intended) of Money.Ecto.Composite.Type.cast/2.

I’ve published documentation for Money.Ecto.Composite.Type.cast/2 and would welcome any PRs that improve it.

Where Next?

Popular in Questions 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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement