slouchpie

slouchpie

Cldr.Number custom formatter

Hi

I have been going through the Cldr docs and I don’t see a clear way to do this.

How can I configure Cldr so I can overwrite the to_string function for certain things?

In particular I want to write the formatter so money:

<%= @money %>

is displayed as "$20" if the amount is {:USD, 20} and displayed as "$20.10" if the amount is {:USD, "20.10"}.
I know I can use Money.to_string with formatting options but I want to keep it simple for other devs and have custom Cldr formatter.

Anyone have any idea or pointers to help me out?

Marked As Solved

kip

kip

ex_cldr Core Team

I have pushed commit 72f78b4 to ex_money master branch implementing your suggestion. The changelog entry reads:

Enhancements

  • Adds configuration option :exclude_protocol_implementations to omit generating implementations for one or more protocols from the list Json.Encoder, Phoneix.HTML.Safe and Gringotts.Money. Thanks to @jgough-playoxygen for the suggestion.

If you have a chance to take it for a spin from GitHub that would be great. Pending your feedback I’ll then publish to hex.

Also Liked

LostKobrakai

LostKobrakai

If this in phoenix templates? If so then you likely want to implement Phoenix.HTML.Safe for the Money struct.

slouchpie

slouchpie

For posterity, my function now looks like this:

  defimpl Phoenix.HTML.Safe, for: Money do
    def to_iodata(%Money{amount: %Decimal{coef: coef, exp: 0}} = money) when is_integer(coef) do
      money
      |> Money.to_string!(fractional_digits: 0)
      |> HTML.Safe.to_iodata()
    end

    @simple_currencies [:USD, :EUR]
    def to_iodata(%Money{amount: %Decimal{coef: coef, exp: -2}, currency: currency} = money)
        when rem(coef, 100) == 0 and currency in @simple_currencies do
        money
        |> Money.to_string!(fractional_digits: 0)
        |> HTML.Safe.to_iodata()
      end

    def to_iodata(money) do
      money
      |> Money.to_string!()
      |> HTML.Safe.to_iodata()
    end
  end
kip

kip

ex_cldr Core Team

Oh, interesting. I’ll look at adding that as an implementation on the next version of ex_money. I’m only just now getting back to web dev and hadn’t realised there is both the Html.Safe and Phoenix.HTML.Safe protocols (and maybe there isn’t a difference any more!)

D’Oh, should have read my own code first. This is the implementation:

  defimpl Phoenix.HTML.Safe, for: Money do
    def to_iodata(money) do
      Phoenix.HTML.Safe.to_iodata(Money.to_string!(money))
    end
  end

Does it not do what you expect?

You can actually store formatting options in a Money.t if you need - that capability was primarily added to support this protocol. Of course if thats not enough you can certainly do your own implementation as you showed.

Example

iex> m = Money.new(:USD, 1234, format: :long)
#Money<:USD, 1234>
iex> Money.to_string m                       
{:ok, "1,234 US dollars"}
kip

kip

ex_cldr Core Team

Thanks for the explanation and I understand the use case. Let me think on this and revert soon.

kip

kip

ex_cldr Core Team

As of commit 8c14f4f there is a compile-time option to configure whether or not the default protocol implementation is defined. The changelog entry is:

  • Add :define_phoenix_html_safe as a compile-time configuration option. The default is true. If set to a falsy value then the default protocol implementation will not be generated and users can define their own implementation.

Basically, in config.exs and friends:

config :ex_money,
  define_phoenix_html_safe: false

The commit is on a development branch that will be published Lunar New Year’s Day (next Tuesday) and therefore can’t really be used easily as a GitHub dependency.

If the need becomes urgent I can do a backport to the current main branch and publish a release, but I’d prefer to wait until Tuesday if thats possible. Let me know?

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
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
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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

We're in Beta

About us Mission Statement