Transform input to a currency. The actual values are numeric, but will be printed as formatted currency values.

as.currency(x, currency_symbol = Sys.localeconv()["int_curr_symbol"], ...)

is.currency(x)

# S3 method for class 'currency'
print(
  x,
  decimal.mark = getOption("OutDec"),
  big.mark = ifelse(decimal.mark == ",", ".", ","),
  as_symbol = FALSE,
  ...
)

# S3 method for class 'currency'
format(
  x,
  currency_symbol = attributes(x)$currency_symbol,
  decimal.mark = getOption("OutDec"),
  big.mark = ifelse(decimal.mark == ",", ".", ","),
  as_symbol = FALSE,
  ...
)

Arguments

x

input

currency_symbol

the currency symbol to use, which defaults to the current system locale setting (see Sys.localeconv)

...

other parameters passed on to methods

decimal.mark

symbol to use as a decimal separator, defaults to getOption("OutDec")

big.mark

symbol to use as a thousands separator, defaults to a dot if decimal.mark is a comma, and a comma otherwise

as_symbol

try to format and print using currency symbols instead of text

Details

Printing currency will always have a currency symbol followed by a space, 2 decimal places and is never written in scientific format (like 2.5e+04).

Examples

money <- as.currency(c(0.25, 2.5, 25, 25000))
money
#> [1] `0.25`      `2.50`      `25.00`     `25,000.00`
sum(money)
#> [1] `25,027.75`
max(money)
#> [1] `25,000.00`
mean(money)
#> [1] `6,256.94`

format(money, currency_symbol = "USD")
#> [1] "USD 0.25"      "USD 2.50"      "USD 25.00"     "USD 25,000.00"
format(money, currency_symbol = "EUR", decimal.mark = ",")
#> [1] "EUR 0,25"      "EUR 2,50"      "EUR 25,00"     "EUR 25.000,00"
format(money, currency_symbol = "EUR", as_symbol = TRUE)
#> [1] "€ 0.25"      "€ 2.50"      "€ 25.00"     "€ 25,000.00"

as.currency(2.5e+04)
#> [1] `25,000.00`