This can be helpful when writing reports with knitr/rmarkdown if
we want to print numbers as English words in the output. The function n2w()
is an alias of numbers_to_words().
Usage
numbers_to_words(x, cap = FALSE, hyphen = TRUE, and = FALSE)
n2w(x, cap = FALSE, hyphen = TRUE, and = FALSE)Arguments
- x
A numeric vector. The absolute values should be less than
1e15.- cap
Whether to capitalize the first letter of the word. This can be useful when the word is at the beginning of a sentence. Default is
FALSE.- hyphen
Whether to insert hyphen (-) when the number is between 21 and 99 (except 30, 40, etc.).
- and
Whether to insert
andbetween hundreds and tens, e.g., write 110 as “one hundred and ten” ifTRUEinstead of “one hundred ten”.
Examples
library(xfun)
n2w(0, cap = TRUE)
#> [1] "Zero"
n2w(0:121, and = TRUE)
#> [1] "zero" "one"
#> [3] "two" "three"
#> [5] "four" "five"
#> [7] "six" "seven"
#> [9] "eight" "nine"
#> [11] "ten" "eleven"
#> [13] "twelve" "thirteen"
#> [15] "fourteen" "fifteen"
#> [17] "sixteen" "seventeen"
#> [19] "eighteen" "nineteen"
#> [21] "twenty" "twenty-one"
#> [23] "twenty-two" "twenty-three"
#> [25] "twenty-four" "twenty-five"
#> [27] "twenty-six" "twenty-seven"
#> [29] "twenty-eight" "twenty-nine"
#> [31] "thirty" "thirty-one"
#> [33] "thirty-two" "thirty-three"
#> [35] "thirty-four" "thirty-five"
#> [37] "thirty-six" "thirty-seven"
#> [39] "thirty-eight" "thirty-nine"
#> [41] "forty" "forty-one"
#> [43] "forty-two" "forty-three"
#> [45] "forty-four" "forty-five"
#> [47] "forty-six" "forty-seven"
#> [49] "forty-eight" "forty-nine"
#> [51] "fifty" "fifty-one"
#> [53] "fifty-two" "fifty-three"
#> [55] "fifty-four" "fifty-five"
#> [57] "fifty-six" "fifty-seven"
#> [59] "fifty-eight" "fifty-nine"
#> [61] "sixty" "sixty-one"
#> [63] "sixty-two" "sixty-three"
#> [65] "sixty-four" "sixty-five"
#> [67] "sixty-six" "sixty-seven"
#> [69] "sixty-eight" "sixty-nine"
#> [71] "seventy" "seventy-one"
#> [73] "seventy-two" "seventy-three"
#> [75] "seventy-four" "seventy-five"
#> [77] "seventy-six" "seventy-seven"
#> [79] "seventy-eight" "seventy-nine"
#> [81] "eighty" "eighty-one"
#> [83] "eighty-two" "eighty-three"
#> [85] "eighty-four" "eighty-five"
#> [87] "eighty-six" "eighty-seven"
#> [89] "eighty-eight" "eighty-nine"
#> [91] "ninety" "ninety-one"
#> [93] "ninety-two" "ninety-three"
#> [95] "ninety-four" "ninety-five"
#> [97] "ninety-six" "ninety-seven"
#> [99] "ninety-eight" "ninety-nine"
#> [101] "one hundred" "one hundred and one"
#> [103] "one hundred and two" "one hundred and three"
#> [105] "one hundred and four" "one hundred and five"
#> [107] "one hundred and six" "one hundred and seven"
#> [109] "one hundred and eight" "one hundred and nine"
#> [111] "one hundred and ten" "one hundred and eleven"
#> [113] "one hundred and twelve" "one hundred and thirteen"
#> [115] "one hundred and fourteen" "one hundred and fifteen"
#> [117] "one hundred and sixteen" "one hundred and seventeen"
#> [119] "one hundred and eighteen" "one hundred and nineteen"
#> [121] "one hundred and twenty" "one hundred and twenty-one"
n2w(1e+06)
#> [1] "one million"
n2w(1e+11 + 12345678)
#> [1] "one hundred billion, twelve million, three hundred forty-five thousand, six hundred seventy-eight"
n2w(-987654321)
#> [1] "minus nine hundred eighty-seven million, six hundred fifty-four thousand, three hundred twenty-one"
n2w(1e+15 - 1)
#> [1] "nine hundred ninety-nine trillion, nine hundred ninety-nine billion, nine hundred ninety-nine million, nine hundred ninety-nine thousand, nine hundred ninety-nine"
n2w(123.456)
#> [1] "one hundred twenty-three point four five six"
n2w(123.45678901)
#> [1] "one hundred twenty-three point four five six seven eight nine zero one"
n2w(123.456789098765)
#> [1] "one hundred twenty-three point four five six seven eight nine zero nine eight seven six five"