Skip to content

Commit

Permalink
Honor currency's fractional digits (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Sep 9, 2023
1 parent 16462a6 commit 6e22e52
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Cldr Numbers v2.32.1

This is the changelog for Cldr v2.32.0 released on September 9th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_numbers/tags)

### Bug Fixes

* Use currency default fractional digits for currency formats if no `:fractional_digits` option is provided. Thanks to @dhedlund for the report. This reverts that change made in 2.31.3 which was described as "Don't override currency fractional digits when formatting a number. The format should always define the fractional digits unless overriden by the `:fractional_digits` option." Fractional digits will also be set to the currency definition of digits unless overriden.

## Cldr Numbers v2.32.0

This is the changelog for Cldr v2.32.0 released on September 4th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_numbers/tags)
Expand Down
49 changes: 43 additions & 6 deletions lib/cldr/number/format/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ defmodule Cldr.Number.Format.Options do
options
|> validate_each_option(backend)
|> confirm_currency_format_has_currency()
|> maybe_adjust_currency_format(options.currency, options.format)
|> maybe_adjust_currency_format(options.currency, options.format, backend)
|> resolve_standard_format(backend)
|> maybe_expand_currency_symbol(number)
|> maybe_apply_alpha_next_to_number(backend)
Expand Down Expand Up @@ -172,24 +172,61 @@ defmodule Cldr.Number.Format.Options do
# :narrow, the format to :currency

@doc false
defp maybe_adjust_currency_format(options, currency, :narrow) when not is_nil(currency) do
defp maybe_adjust_currency_format(options, currency, :narrow, backend) when not is_nil(currency) do
currency_format = derive_currency_format(options)

options
|> Map.put(:currency_symbol, :narrow)
|> Map.put(:format, currency_format)
|> Map.put(:currency_format, currency_format)
|> set_default_fractional_digits(backend)
end

# We keep a record of whether the currency format is :currency or :accounting
# because later on we may need to adjust the "alpha_next_to_number" format

defp maybe_adjust_currency_format(%{format: format} = options, currency, _)
defp maybe_adjust_currency_format(%{format: format} = options, currency, _format, backend)
when not is_nil(currency) and format in [:accounting, :currency] do
Map.put(options, :currency_format, options.format)
options
|> Map.put(:currency_format, options.format)
|> set_default_fractional_digits(backend)
end

defp maybe_adjust_currency_format(options, _currency, _format, _backend) do
options
end

defp set_default_fractional_digits(%{fractional_digits: nil, currency_digits: :accounting} = options, backend) do
case Cldr.Currency.currency_for_code(options.currency, backend, locale: options.locale) do
{:ok, currency} ->
Map.put(options, :fractional_digits, currency.digits)

_other ->
options
end
end

defp set_default_fractional_digits(%{fractional_digits: nil, currency_digits: :cash} = options, backend) do
case Cldr.Currency.currency_for_code(options.currency, backend, locale: options.locale) do
{:ok, currency} ->
Map.put(options, :fractional_digits, currency.cash_digits)

_other ->
options
end
end

defp set_default_fractional_digits(%{fractional_digits: nil, currency_digits: :iso} = options, backend) do
case Cldr.Currency.currency_for_code(options.currency, backend, locale: options.locale) do
{:ok, currency} ->
Map.put(options, :fractional_digits, currency.iso_digits)

_other ->
options
end
end

defp maybe_adjust_currency_format(options, _currency, _format) do
defp set_default_fractional_digits(options, _backend) do
options
end

Expand Down Expand Up @@ -388,7 +425,7 @@ defmodule Cldr.Number.Format.Options do
System.system_name_from(number_system, locale, backend)
end

# Currency validation returns a t: Cldr.Currency.t/0
# Currency validation returns a t:Cldr.Currency.t/0

defp validate_option(:currency, %{locale: locale}, backend, :from_locale) do
currency_from_locale(locale, backend)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Cldr.Numbers.Mixfile do

use Mix.Project

@version "2.32.0"
@version "2.32.1"

def project do
[
Expand Down
4 changes: 4 additions & 0 deletions test/support/number_format_test_data.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ defmodule Cldr.Test.Number.Format do
{1234, "COP 1,234.00", [currency: :COP, currency_digits: :iso]},
{1234, "COP 1,234.00", [currency: :COP]},

# Currency default fractional digits
{1234, "¥1,234", [currency: :JPY]},
{1234, "TND 1,234.000", [currency: :TND]},

# Currency with varying currency symbol
{1234, "CUSTOM 1,234.00", [currency: :AUD, currency_symbol: "CUSTOM"]},
{1234, "$1,234.00", [currency: :AUD, currency_symbol: :narrow]},
Expand Down

0 comments on commit 6e22e52

Please sign in to comment.