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

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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement