Skip to contents

Convert an integer vector, or numeric vector which is coercible to an integer without loss of precision, to a cardinal numeral (e.g. one, two, three).

integerish_friendly_safe() checks that all arguments are of the correct type and raises an informative error otherwise. integerish_friendly() does not perform input validation to maximize its speed.

Usage

integerish_friendly(
  numbers,
  zero = "zero",
  na = "missing",
  nan = "not a number",
  inf = "infinity",
  negative = "negative ",
  and = FALSE,
  hyphenate = TRUE
)

integerish_friendly_safe(
  numbers,
  zero = "zero",
  na = "missing",
  nan = "not a number",
  inf = "infinity",
  negative = "negative ",
  and = FALSE,
  hyphenate = TRUE
)

Arguments

numbers

[integer / numeric]

An integer or integer-ish numeric vector to translate.

zero

[character(1)]

What to call values of 0 in numbers (e.g. zero = "zero").

na

[character(1)]

What to call values of NA in numbers (e.g. na = "missing").

nan

[character(1)]

What to call values of NaN in numbers (e.g. nan = "undefined").

inf

[character(1)]

What to call values of Inf in numbers (e.g. inf = "infinity").

negative

[character(1)]

A prefix added to the translation of negative elements of numbers. negative is the string "negative " by default.

and

[TRUE / FALSE]

Whether to insert an " and " before the tens place of translated numbers. and is FALSE by default.

hyphenate

[TRUE / FALSE]

Whether to hyphenate numbers 21 through 99 (e.g. "twenty-one" vs. "twenty one"). hyphenate is TRUE by default.

Value

A non-NA character vector of the same length as numbers.

Examples

integerish_friendly(c(0, 1, 2, NA, NaN, Inf, -Inf))
#> [1] "zero"              "one"               "two"              
#> [4] "missing"           "not a number"      "infinity"         
#> [7] "negative infinity"
integerish_friendly(10^10)
#> [1] "ten billion"

# Specify the translations of "special" numbers
integerish_friendly(-10, negative = "minus ")
#> [1] "minus ten"
integerish_friendly(NaN, nan = "undefined")
#> [1] "undefined"

# Modify the output formatting
integerish_friendly(1234)
#> [1] "one thousand two hundred thirty-four"
integerish_friendly(1234, and = TRUE)
#> [1] "one thousand two hundred and thirty-four"
integerish_friendly(1234, hyphenate = FALSE)
#> [1] "one thousand two hundred thirty four"

# Input validation
try(integerish_friendly_safe(0.5))
#> Error : `numbers` must be coercible to an integer without loss of precision.
try(integerish_friendly_safe(1L, na = TRUE))
#> Error : `na` must be a string, not `TRUE`.