How to express an integer number in English words?
Mathematica 10 introduces IntegerName
:
IntegerName[n]
gives a string containing the full English name of the integer n.
IntegerName[n,"type"]
gives a string of the specified type.Possible types include:
"DigitsWords"
a combination of three-digit numbers and words"Words"
using only words"Approximate"
the first few digits plus thousands, millions, etc."ApproximateWords"
the first few digits as words plus thousands, etc.
Example:
IntegerName[#, "Words"] & /@
{123456, 1000000001, 123456789123456789} // Column
"one hundred twenty-three thousand, four hundred fifty-six" "one billion, one" "one hundred twenty-three quadrillion, four hundred fifty-six trillion, seven hundred eighty-nine billion, one hundred twenty-three million, four hundred fifty-six thousand, seven hundred eighty-nine"
Nested WolframAlpha
approach, showing the intermediate steps:
numberString[a_, k_: 10] :=
FixedPointList[
StringReplace[#,
b : (DigitCharacter ..) :>
WolframAlpha["spell " <> b, {{"Result", 1}, "Plaintext"}]] &, a,
k]
numberString["123456"]
(*
==> {"123456", "123 thousand and 456", "one hundred twenty-three \
thousand and four hundred fifty-six", "one hundred twenty-three \
thousand and four hundred fifty-six"}
*)
numberString["123456789123456789123456789"]
(*
==> {"123456789123456789123456789", "123 septillion, 456 \
sextillion, 789 quintillion, 123 quadrillion, 456 trillion, 789 \
billion, 123 million, 456 thousand and 789", "one hundred \
twenty-three septillion, four hundred fifty-six sextillion, seven \
hundred eighty-nine quintillion, one hundred twenty-three \
quadrillion, four hundred fifty-six trillion, seven hundred \
eighty-nine billion, one hundred twenty-three million, four hundred \
fifty-six thousand and seven hundred eighty-nine", "one hundred \
twenty-three septillion, four hundred fifty-six sextillion, seven \
hundred eighty-nine quintillion, one hundred twenty-three \
quadrillion, four hundred fifty-six trillion, seven hundred \
eighty-nine billion, one hundred twenty-three million, four hundred \
fifty-six thousand and seven hundred eighty-nine"}
*)
Messy but a working method
inWords[n_] := Module[
{r,
numNames = {"", " one", " two", " three", " four", " five",
" six", " seven", " eight", " nine"},
teenNames = {" ten", " eleven", " twelve", " thirteen",
" fourteen", " fifteen", " sixteen", " seventeen", " eighteen",
" nineteen"},
tensNames = {"", " ten", " twenty", " thirty", " forty", " fifty",
" sixty", " seventy", " eighty", " ninety"},
decimals = {"", " thousand", " million", " billion", " trillion",
" quadrillion", " quintillion", " sextillion", " septillion",
" octillion", " nonillion", " decillion", " undecillion",
" duodecillion", " tredecillion", " quattuordecillion",
" quindecillion", " sexdecillion", " septendecillion",
" octodecillion", " Novemdecillion", " Vigintillion", " 66illion",
" 69illion"}
}
,
r = If[# != 0,
numNames[[# + 1]] <> " hundred" <>
If[#2 != 0 || #3 != 0, " and", ""], ""]
<>
Switch[#2
, 0, numNames[[#3 + 1]]
, 1, teenNames[[#3 + 1]]
, _, tensNames[[#2 + 1]] <> numNames[[#3 + 1]]
]
& @@@
(PadLeft[FromDigits /@ Characters@StringReverse@#,
3] & /@ StringCases[StringReverse@IntegerString@n,
RegularExpression["\\d{1,3}"]]);
StringJoin@
Reverse@
MapThread[
If[# != "", StringJoin[##], ""] &, {r, Take[decimals, Length@r]}]
];
Output
In[279]:= FromDigits@Table[RandomInteger[9], {70}]
inWords@%
Out[279]= \
7317782180245641104170634561625559007936487060051082174021876603737988
Out[280]= " seven 69illion three hundred and seventeen 66illion seven \
hundred and eighty two Vigintillion one hundred and eighty \
Novemdecillion two hundred and forty five octodecillion six hundred \
and forty one septendecillion one hundred and four sexdecillion one \
hundred and seventy quindecillion six hundred and thirty four \
quattuordecillion five hundred and sixty one tredecillion six hundred \
and twenty five duodecillion five hundred and fifty nine undecillion \
seven decillion nine hundred and thirty six nonillion four hundred \
and eighty seven octillion sixty septillion fifty one sextillion \
eighty two quintillion one hundred and seventy four quadrillion \
twenty one trillion eight hundred and seventy six billion six hundred \
and three million seven hundred and thirty seven thousand nine \
hundred and eighty eight"