This question already has an answer here:

Given the written, spelled-out representation of an integer from 1 to 10^15-1 inclusive, convert it to an actual integer in decimal.

Input Format

The input will always be a string of groups, where each group is composed only of letters and spaces(). The groups are separated by commas.

You may choose whether you want the letters to be all lowercase, all uppercase, or title case.

For the purposes of this challenge, there are no hyphens (-) between e.g. "forty-two", so tokens within groups are separated by spaces only.

The input will contain no words and.

You can test if an input is valid by putting it as input into this Retina program.

Here is a full table of tokens:


+1 | "one"
+2 | "two"
+3 | "three"
+4 | "four"
+5 | "five"
+6 | "six"
+7 | "seven"
+8 | "eight"
+9 | "nine"


+10 | "ten"
+11 | "eleven"
+12 | "twelve"
+13 | "thirteen"
+14 | "fourteen"
+15 | "fifteen"
+16 | "sixteen"
+17 | "seventeen"
+18 | "eighteen"
+19 | "nineteen


+20 | "twenty"
+30 | "thirty"
+40 | "forty"
+50 | "fifty"
+60 | "sixty"
+70 | "seventy"
+80 | "eighty"
+90 | "ninety"


×100 | "hundred"

Place markings:

×10^3  | "thousand"
×10^6  | "million"
×10^9  | "billion"
×10^12 | "trillion

How to Evaluate a Spelled-out Representation

We will be working with the example input "forty five thousand,one hundred thirteen"

First, split the input into groups by commas, to get e.g. ["forty five thousand","one hundred thirteen"]

Then, evaluate each group:

  1. Split by spaces to get a list of tokens
  2. Start with a running total of 0
  3. For each token, do as the table says. If the token is 9, do +9 and add 9 to the running total. If the token is thousand, multiply the running total by 1000.

For the second example group, this would be:

running total    token
1                "one"
100              "hundred"
113              "thirteen"

For the first example group, this yields 45000.

After this is done, sum each group to get the output e.g.45113.

Note that you do not have to evaluate the number exactly as I had laid it out as long as you produce the same results. For example, you could do string substitution to get an expression you can eval.

Test Cases

"one" -> 1
"ten" -> 10
"forty" -> 40
"one hundred" -> 100
"one thousand" -> 1000
"ten million" -> 10000000
"one hundred billion" -> 100000000000
"ten trillion" -> 10000000000000
"nine hundred ninety nine trillion,nine hundred ninety nine billion,nine hundred ninety nine million,nine hundred ninety nine thousand,nine hundred ninety nine" -> 999999999999999
"two hundred forty one trillion,eight hundred sixty one billion,seventy seven million,one hundred fifty four thousand,twenty eight" -> 241861077154028
"four hundred eighty five trillion,six hundred fifty seven billion,six hundred million,four hundred eighty two thousand,one hundred seventy eight" -> 485657600482178
"three hundred eighty nine trillion,five hundred twenty billion,one hundred ninety seven million,nine hundred fifty thousand,nine hundred eighty four" -> 389520197950984
"one hundred fifty five trillion,five hundred five billion,four hundred fifty eight million,six hundred seventy thousand,six hundred sixty one" -> 155505458670661
"one hundred fifty two trillion,ninety three billion,two hundred twenty one million,nine hundred thirty two thousand,five hundred eighty" -> 152093221932580
"three hundred forty three trillion,two hundred eighty six billion,nine hundred sixty five million,six hundred four thousand,two hundred seventy nine" -> 343286965604279
"six hundred ninety eight trillion,nine hundred seventy five billion,one hundred five million,sixty thousand,two hundred forty" -> 698975105060240
"five hundred seventy four trillion,one hundred sixty billion,five hundred ninety eight million,one hundred nine thousand,sixty two" -> 574160598109062
"four hundred seventy seven trillion,two hundred twenty five billion,eight hundred sixty three million,one hundred eighteen thousand,eight hundred four" -> 477225863118804
"eighty seven trillion,two hundred eighty three billion,six hundred eighty eight million,eight hundred fifty three thousand,sixty nine" -> 87283688853069
"eight hundred trillion,fifty billion,five hundred seven million,eighty thousand,fifty" -> 800050507080050
"seven hundred seventy three trillion,three hundred billion,four hundred thousand,nine" -> 773300000400009
"three hundred trillion,two hundred eighty eight billion" -> 300288000000000
"four hundred trillion,two hundred six billion,sixty six million,sixty" -> 400206066000060
"eight trillion,ninety nine million,four hundred sixty thousand,four hundred" -> 8000099460400
"seventy trillion,six hundred billion,forty million,seven hundred thousand" -> 70600040700000
"one hundred twenty billion,one hundred million,seventy thousand,forty" -> 120100070040
"four billion,eight hundred million,five hundred four thousand,five hundred six" -> 4800504506
"five hundred trillion,three hundred fifty five billion,seventy six thousand" -> 500355000076000
"fifty eight trillion,ninety eight billion,forty" -> 58098000000040

Not a Duplicate

The other question involves the units only modifying the previous value, such that every 'group' is composed of only an integer and a modifier. This requires a more dynamic approach in order to calculate groups of varying sizes.


marked as duplicate by pppery, Sriotchilism O'Zaic, Downgoat code-golf Aug 28 '17 at 4:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Sandbox : Here \$\endgroup\$ – fireflame241 Aug 28 '17 at 0:37
  • \$\begingroup\$ Er, that challenge involves interpreting units in a similar way, and then asks you to print the result with some other system. Answers to that challenger generally do the two parts seperately, which means they can be copied with the second half removed \$\endgroup\$ – pppery Aug 28 '17 at 0:52

Mathematica, 28 bytes

Mathematica has a builtin for everything


Of course, Mathematica has a built-in interpreter for spelled-out numbers.

Try it on Wolfram Sandbox




f["three hundred trillion,two hundred eighty eight billion"]
f["five hundred seventy four trillion,one hundred sixty billion,five hundred ninety eight million,one hundred nine thousand,sixty two"]
  • 5
    \$\begingroup\$ I really don't like these answers, but I won't downvote it as there's nothing wrong with it. \$\endgroup\$ – Stan Strum Aug 28 '17 at 1:00
  • 5
    \$\begingroup\$ @StanStrum uh... okay? I think your comment is more suitable in chat (or meta posts), not comments. It doesn't add any value to the answer. \$\endgroup\$ – JungHwan Min Aug 28 '17 at 1:02

Python 2, 491 bytes

for i in s.split(','):
 for k in i.split():j=j+a[k]if k in a else j*m[k]
print r

Try it online!

  • 1
    \$\begingroup\$ You can save some bytes by writing r as dict(one=1... \$\endgroup\$ – pppery Aug 28 '17 at 2:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.