The fmt_currency() function

Let’s make a simple gt table from the exibble dataset. We’ll keep only the num and currency, columns, then, format those columns using fmt_currency() (with the "JPY" and "GBP" currencies).

exibble |>
  dplyr::select(num, currency) |>
  gt() |>
  fmt_currency(
    columns = num,
    currency = "JPY"
  ) |>
  fmt_currency(
    columns = currency,
    currency = "GBP"
  )
num currency
¥0 £49.95
¥2 £17.95
¥33 £1.39
¥444 £65,100.00
¥5,550 £1,325.81
NA £13.26
¥777,000 NA
¥8,880,000 £0.44

Let’s take a single column from exibble (currency) and format it with a currency name (this differs from the 3-letter currency code). In this case, we’ll use the "euro" currency and set the placement of the symbol to the right of any value. Additionally, the currency symbol will separated from the value with a single space character (using incl_space = TRUE).

exibble |>
  dplyr::select(currency) |>
  gt() |>
  fmt_currency(
    currency = "euro",
    placement = "right",
    incl_space = TRUE
  )
currency
49.95 €
17.95 €
1.39 €
65,100.00 €
1,325.81 €
13.26 €
NA
0.44 €

With the pizzaplace dataset, let’s make a summary table that gets the number of "hawaiian" pizzas sold (and revenue generated) by month. In the gt table, we’ll format only the revenue column. The currency value is automatically U.S. Dollars when don’t supply either a currency code or a locale. We’ll also create a grand summary with grand_summary_rows(). Within that summary row, the total revenue needs to be formatted with fmt_currency() and we can do that within the fmt argument.

pizzaplace |>
  dplyr::filter(name == "hawaiian") |>
  dplyr::mutate(month = lubridate::month(date, label = TRUE, abbr = TRUE)) |>
  dplyr::select(month, price) |>
  dplyr::group_by(month) |>
  dplyr::summarize(
    `number sold` = dplyr::n(),
    revenue = sum(price)
  ) |>
  gt(rowname_col = "month") |>
  tab_header(title = "Summary of Hawaiian Pizzas Sold by Month") |>
  fmt_currency(columns = revenue) |>
  grand_summary_rows(
    fns = list(label = "Totals:", id = "totals", fn = "sum"),
    fmt = ~ fmt_currency(., columns = revenue),
  ) |>
  opt_all_caps()
Summary of Hawaiian Pizzas Sold by Month
number sold revenue
Jan 185 $2,442.75
Feb 198 $2,633.00
Mar 217 $2,878.50
Apr 219 $2,867.75
May 198 $2,688.00
Jun 189 $2,563.75
Jul 195 $2,620.25
Aug 201 $2,678.75
Sep 196 $2,616.25
Oct 188 $2,514.75
Nov 227 $2,952.75
Dec 209 $2,816.75
Totals: 2422 $32,273.25

If supplying a locale value to fmt_currency(), we can opt use the locale’s assumed currency and not have to supply a currency value (doing so would override the locale’s default currency). With a column of locale values, we can format currency values on a row-by-row basis through the use of from_column(). Here, we’ll reference the locale column in the argument of the same name.

dplyr::tibble(
  amount = rep(50.84, 5),
  currency = c("JPY", "USD", "GHS", "KRW", "CNY"),
  locale = c("ja", "en", "ee", "ko", "zh"),
) |>
  gt() |>
  fmt_currency(
    columns = amount,
    locale = from_column(column = "locale")
  ) |>
  cols_hide(columns = locale)
amount currency
¥51 JPY
$50.84 USD
₵50.84 GHS
₩51 KRW
¥50.84 CNY

We can similarly use from_column() to reference a column that has currency code values. Here’s an example of how to create a simple currency conversion table. The curr column contains the 3-letter currency codes, and that column is referenced via from_column() in the currency argument of fmt_currency().

dplyr::tibble(
  flag = c("EU", "GB", "CA", "AU", "JP", "IN"),
  curr = c("EUR", "GBP", "CAD", "AUD", "JPY", "INR"),
  conv = c(
    0.912952, 0.787687, 1.34411,
    1.53927, 144.751, 82.9551
  )
) |>
  gt() |>
  fmt_currency(
    columns = conv,
    currency = from_column(column = "curr")
  ) |>
  fmt_flag(columns = flag) |>
  cols_merge(columns = c(flag, curr)) |>
  cols_label(
    flag = "Currency",
    conv = "Amount"
  ) |>
  tab_header(
    title = "Conversion of 1 USD to Six Other Currencies",
    subtitle = md("Conversion rates obtained on **Aug 13, 2023**")
  )
Conversion of 1 USD to Six Other Currencies
Conversion rates obtained on Aug 13, 2023
Currency Amount
EUR €0.91
GBP £0.79
CAD $1.34
AUD $1.54
JPY ¥145
INR ₹82.96