Translate a biginteger to a cardinal character vector
Source:R/biginteger-friendly.R
biginteger_friendly.Rd
Convert a <bignum_biginteger>
to a cardinal numeral (e.g. one, two, three).
A bignum::biginteger()
can store any integer (i.e. arbitrary precision),
which is useful for manipulating numbers too large to be represented (accurately)
in an <integer>
or <numeric>
vector.
biginteger_friendly_safe()
checks that all arguments are of the correct type
and raises an informative error otherwise. biginteger_friendly()
does not
perform input validation to maximize its speed.
Usage
biginteger_friendly(
numbers,
zero = "zero",
na = "missing",
nan = "not a number",
inf = "infinity",
negative = "negative ",
and = FALSE,
hyphenate = TRUE
)
biginteger_friendly_safe(
numbers,
zero = "zero",
na = "missing",
nan = "not a number",
inf = "infinity",
negative = "negative ",
and = FALSE,
hyphenate = TRUE
)
Arguments
- numbers
[bignum_biginteger]
A
bignum::biginteger()
vector to translate.- zero
[character(1)]
What to call values of
0
innumbers
(e.g.zero = "zero"
).- na
[character(1)]
What to call values of
NA
innumbers
(e.g.na = "missing"
).- nan
[character(1)]
What to call values of
NaN
innumbers
(e.g.nan = "undefined"
).- inf
[character(1)]
What to call values of
Inf
innumbers
(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 translatednumbers
.and
isFALSE
by default.- hyphenate
[TRUE / FALSE]
Whether to hyphenate numbers 21 through 99 (e.g.
"twenty-one"
vs."twenty one"
).hyphenate
isTRUE
by default.
Examples
biginteger_friendly(bignum::biginteger(c(0, 1, 2, NA, 10001)))
#> [1] "zero" "one" "two" "missing"
#> [5] "ten thousand one"
# Specify the translations of "special" numbers
biginteger_friendly(bignum::biginteger(-10), negative = "minus ")
#> [1] "minus ten"
biginteger_friendly(bignum::biginteger(NA), na = "unknown")
#> [1] "unknown"
# Modify the output formatting
biginteger_friendly(bignum::biginteger(9999))
#> [1] "nine thousand nine hundred ninety-nine"
biginteger_friendly(bignum::biginteger(9999), and = TRUE)
#> [1] "nine thousand nine hundred and ninety-nine"
biginteger_friendly(bignum::biginteger(9999), hyphenate = FALSE)
#> [1] "nine thousand nine hundred ninety nine"
# Translate large numbers
large <- bignum::biginteger(10L)^1001L
biginteger_friendly(large)
#> [1] "one hundred duotrigintatrecentillion"
# Input validation
try(biginteger_friendly_safe(1L))
#> Error : `numbers` must be of class <bignum_biginteger>, not <integer>.