Translate a bigfloat to a cardinal character vector
Source:R/bigfloat-friendly.R
bigfloat_friendly.RdConvert a <bignum_bigfloat> to a cardinal numeral (e.g. one tenth, one, two).
A bignum::bigfloat() can store numbers with up to 50 decimal digits of
precision, which is useful for manipulating numbers which can't be accurately
represented in a <numeric> vector.
bigfloat_friendly_safe() checks that all arguments are of the correct type
and raises an informative error otherwise. bigfloat_friendly() does not
perform input validation to maximize its speed.
Usage
bigfloat_friendly(
numbers,
zero = "zero",
na = "missing",
nan = "not a number",
inf = "infinity",
negative = "negative ",
decimal = " and ",
and = FALSE,
hyphenate = TRUE,
and_fractional = and,
hyphenate_fractional = hyphenate,
english_fractions = NULL
)
bigfloat_friendly_safe(
numbers,
zero = "zero",
na = "missing",
nan = "not a number",
inf = "infinity",
negative = "negative ",
decimal = " and ",
and = FALSE,
hyphenate = TRUE,
and_fractional = and,
hyphenate_fractional = hyphenate,
english_fractions = NULL
)Arguments
- numbers
[bignum_bigfloat]A
bignum::bigfloat()vector to translate.- zero
[character(1)]What to call values of
0innumbers(e.g.zero = "zero").- na
[character(1)]What to call values of
NAinnumbers(e.g.na = "missing").- nan
[character(1)]What to call values of
NaNinnumbers(e.g.nan = "undefined").- inf
[character(1)]What to call values of
Infinnumbers(e.g.inf = "infinity").- negative
[character(1)]A prefix added to the translation of negative elements of
numbers.negativeis the string"negative "by default.- decimal
[character(1)]A word inserted between the whole and fractional part of translated
numbers.decimalis the string" and "by default.- and
[TRUE / FALSE]Whether to insert an
" and "before the tens place of translatednumbers.andisFALSEby default.- hyphenate
[TRUE / FALSE]Whether to hyphenate numbers 21 through 99 (e.g.
"twenty-one"vs."twenty one").hyphenateisTRUEby default.- and_fractional
[TRUE / FALSE]Whether to insert an
" and "before the smallest fractional tens place of translatednumbers(e.g."one hundred one thousandths"vs."one hundred and one thousandths").and_fractionalis equal toandby default.- hyphenate_fractional
[TRUE / FALSE]Whether to hyphenate numbers 21 through 99 in the fractional part of translated
numbers(e.g."twenty-one hundredths"or"twenty one hundredths"). This also determines the hyphenation of the fractional units (e.g."one ten-millionth"vs."one ten millionth").hyphenate_fractionalis equal tohyphenateby default.- english_fractions
[character]A named character vector used as a dictionary for the translation of the fractional part of
numbers. The names (i.e. keys) are the decimal digits of a fractional number and the values are the corresponding translations.For example
english_fractions = c("5" = "a half")matches the number0.5(translated as"a half") and2.5(translated as"two and a half").By default
english_fractionsis a named character vector with translations for fractionsx / yforx = 1, 2, ..., 8andy = 1, 2, ..., 9. For example,2 / 3is translated as"two thirds"and1 / 2is translated as"one half".Provide an empty character to
english_fractionsto opt out of any such translations. In this case1 / 2is translated as"five tenths"instead of"one half".
Examples
bigfloat_friendly(bignum::bigfloat(c(0.5, 0, 0.123, NA, NaN, Inf)))
#> [1] "one half"
#> [2] "zero"
#> [3] "one hundred twenty-three thousandths"
#> [4] "missing"
#> [5] "not a number"
#> [6] "infinity"
# Specify the translations of "special" numbers
bigfloat_friendly(bignum::bigfloat(NaN), nan = "NAN")
#> [1] "NAN"
# Modify the output formatting
big <- bignum::bigfloat(1234.5678)
bigfloat_friendly(big)
#> [1] "one thousand two hundred thirty-four and five thousand six hundred seventy-eight ten-thousandths"
bigfloat_friendly(big, decimal = " point ")
#> [1] "one thousand two hundred thirty-four point five thousand six hundred seventy-eight ten-thousandths"
bigfloat_friendly(big, hyphenate_fractional = FALSE)
#> [1] "one thousand two hundred thirty-four and five thousand six hundred seventy eight ten thousandths"
bigfloat_friendly(big, and = TRUE, and_fractional = TRUE, decimal = " . ")
#> [1] "one thousand two hundred and thirty-four . five thousand six hundred and seventy-eight ten-thousandths"
# The `friendlynumber.bigfloat.digits` option specifies the number of
# `<bignum_bigfloat>` digits mentioned by `bigfloat_friendly()`
opts <- options()
options(friendlynumber.bigfloat.digits = 5)
bigfloat_friendly(bignum::bigpi)
#> [1] "three and fourteen thousand one hundred fifty-nine hundred-thousandths"
options(friendlynumber.bigfloat.digits = 10)
bigfloat_friendly(bignum::bigpi)
#> [1] "three and one billion four hundred fifteen million nine hundred twenty-six thousand five hundred thirty-six ten-billionths"
options(opts)
# Set `english_fractions` to specify the translation of certain
# fractions. The names (keys) of `english_fractions` should match
# the decimal part of a fraction (e.g. `"04"` matches `0.04`).
bigfloat_friendly(
bignum::bigfloat(c(1/2, 0.04, 1.5, 10)),
english_fractions = c(`5` = "1/2", `04` = "4/100")
)
#> [1] "1/2" "4/100" "one and 1/2" "ten"
# Input validation
try(bigfloat_friendly_safe(bignum::bigpi, and = NA))
#> Error : `and` must be `TRUE` or `FALSE`, not `NA`.