Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow user to set a preferred currency #1346

Closed
2 tasks done
ThierryO opened this issue Jun 7, 2023 · 2 comments · Fixed by #1347
Closed
2 tasks done

allow user to set a preferred currency #1346

ThierryO opened this issue Jun 7, 2023 · 2 comments · Fixed by #1347

Comments

@ThierryO
Copy link

ThierryO commented Jun 7, 2023

Prework

Proposal

fmt_currency() uses the default currency = "USD". Allow the user to set a different default. Maybe something simple like currency = getOption("gt.currency", default = "USD"). When the user doesn't set gt.currency, the default currency is USD.

@rich-iannone
Copy link
Member

I admit that the current implementation is not what I wanted so I'm quite glad you made the issue for this! I don't really prefer using options (I know there are a few, but I hope they are for non-essentials) because it tends to be bad for reproducibility. However, I do have an idea for this.

We have the locale argument in the gt() function. By default, it's set to NULL but you could use "fr-BE" and, from that, we can obtain the territory's current currency. Here's an example:

exibble |>
  dplyr::select(num, currency) |>
  gt(locale = "fr-BE") |>
  fmt_currency(
    currency = "EUR" # <- this line wouldn't be needed, `"EUR"` would be inferred from the `fr-BE` locale
  )

The CLDR (which gt makes heavy use of) has data on currencies per territory: https://github.com/unicode-org/cldr-json/blob/fb61c0afc2b49264b32d5d7bae6f660dd2ebee34/cldr-json/cldr-core/supplemental/currencyData.json. It's not a bad practice to obtain the currency code from the locale (several posts on different forums confirms that functionality written to do this is used often, e.g., https://stackoverflow.com/questions/8325002/how-to-get-the-default-currency-from-the-php-intl-icu-library).

This would involve making currency = NULL in fmt_currency() which is not bad at all. If locale is not specified in gt() or fmt_currency() then the default locale ("en") would be used, resulting in "USD" (same as before).

With these changes, you'd only need to specify the currency code to override the locale-provided currency. I know there are some strange cases where there are multiple currencies in current use, but that can be handled.

@rich-iannone
Copy link
Member

In addition to this, the info_locales() table should be updated to indicate which territory belongs to a base locale (e.g., "pt" -> "BR", "fr" -> "FR", etc.). Furthermore, the info_currencies() table should have information on the locales associated with each currency code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment