Skip to content

Cldr Numbers version 2.29.0

Compare
Choose a tag to compare
@kipcole9 kipcole9 released this 17 Jan 12:42

Behavior change

  • In prior releases formatting a number as format: :currency would derive the currency code from the locale if no :currency_code was provided. This is no longer the case. As of CLDR42 a format :currency_no_symbol is defined to allow formatting of the number without an associated symbol. Now when format: :currency is passed without a :currency_code option, the format is changed to format: :currency_no_symbol. To retain the existing behaviour, pass currency_code: Cldr.Currency.currency_from_locale(locale) as an option.

  • In prior releases the currency format (:currency or :accounting) would be overriden by any choice expressed in the locale. That is no longer the case from this release. To retain the existing behaviour, pass format: Cldr.Currency.currency_format_from_locale(locale) as an option.

Enhancements

  • Adds an option :wrapper to Cldr.Number.to_string/2. The argument is a 2-arity function that receives the parameters string and tag where tag is one of :number, :currency_symbol, :currency_space, :literal, :quote, :percent, :permille, :minus or :plus. The function must return a string or a "safe string" such as that returned by Phoenix.HTML.Tag.content_tag/3. The function can be used to wrap format elements in HTML or other tags. Thanks to @rubas for the motivation and review. Example:
iex> Cldr.Number.to_string(100, format: :currency, currency: :USD, wrapper: fn
...>   string, :currency_symbol -> "<span class=\"symbol\">" <> string <> "</span>"
...>   string, :number -> "<span class=\"number\">" <> string <> "</span>"
...>   string, :currency_space -> "<span>" <> string <> "</span>"
...>   string, _other -> string
...> end)
{:ok, "<span class=\"symbol\">$</span><span class=\"number\">100.00</span>"}

# It is also possible and recommended to use the `Phoenix.HTML.Tag.content_tag/3` 
# function if wrapping HTML tags since these will ensure HTML entities are 
# correctly encoded.  For example:
iex> Cldr.Number.to_string(100, format: :currency, currency: :USD, wrapper: fn
...>   string, :currency_symbol -> Phoenix.HTML.Tag.content_tag(:span, string, class: "symbol")
...>   string, :number -> Phoenix.HTML.Tag.content_tag(:span, string, class: "number")
...>   string, :currency_space -> Phoenix.HTML.Tag.content_tag(:span, string)
...>   string, _other -> string
...> end)
{:ok, "<span class=\"symbol\">$</span><span class=\"number\">100.00</span>"}
  • Adds the number formats :currency_no_symbol and :accounting_no_symbol that can be used to format currency amounts without an associated currency symbol.

  • Removes the Cldr.Number.to_string/3 requirement for a currency to be provided (or derived) when the format is :currency. If a currency is provided, the format :currency is used. If a currency is not provided, the format :currency_no_symbol is used.