elixir-cldr/cldr_numbers

:fractional_digits must be a positive integer or nil. Found 0

Closed this issue · 3 comments

Hello!

In my app, my goal is to present the user w/ a simple approximation of a currency value:

  @doc """
  Given an amount and currency, render a string for that currency in USD

  ## Examples
  iex> Scorpion.Utils.format_rounded_price_usd(%{amount: "50.00", currency: :USD})
  "$50"
  """
  def format_rounded_price_usd(%{amount: amount, currency: currency}) do
    amount
    |> Money.new(currency)
    |> Money.round(currency_digits: 0)
    |> Money.to_string!(fractional_digits: 0)
  end

When the latest bump happened, this started failing w/ the error :fractional_digits must be a positive integer or nil. Found 0

What is the more-correct way to tell the to_string function to drop all fractional_digits? This code path happens to be USD-Only so I can obviously do this myself w/ some hacks, but curious if I'm missing a simpler solution here..

Thanks!

Sorry for the inconvenience. I completely rewrote options handling for Cldr.Number.to_string/3 and inserted this regression. Fixed now, added tests and published on hex as ex_cldr_numbers 2.13.1. fractional_digits: 0 is restored to its former glory.

Thanks for the bug report.

Appreciate you!

May not be useful to you but there is also Cldr.Number.to_approx_string/3:

iex> Cldr.Number.to_approx_string 50.34, currency: :USD, fractional_digits: 0
{:ok, "~$50"}

There isn't a Money.to_string equivalent so you'd need a helper function to call it but if its useful I'm fine to add Money.to_approx_string/3 too.