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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









