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"}

Last Post!

kip

kip

ex_cldr Core Team

Thanks for the feedback. I’ve published ex_money version 5.8.0 with this new configuration option :exclude_protocol_implementations.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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 records...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
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 records...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New

We're in Beta

About us Mission Statement