title: Convert numbers to English strings
body: <p>Websites like <a href="http://www.easysurf.cc/cnvert18.htm" rel="nofollow">http://www.easysurf.cc/cnvert18.htm</a> and <a href="http://www.calculatorsoup.com/calculators/conversions/numberstowords.php" rel="nofollow">http://www.calculatorsoup.com/calculators/conversions/numberstowords.php</a> tries to convert a numerical string into an english strings, but they are giving natural sounding output.</p>
  
  <p>For example, on <a href="http://www.easysurf.cc/cnvert18.htm" rel="nofollow">http://www.easysurf.cc/cnvert18.htm</a>:</p>
  
  <pre><code>[in]: 100456
  [out]:  one hundred  thousand four hundred fifty-six
  </code></pre>
  
  <p>this website is a little better, <a href="http://www.calculator.org/calculate-online/mathematics/text-number.aspx" rel="nofollow">http://www.calculator.org/calculate-online/mathematics/text-number.aspx</a>:</p>
  
  <pre><code>[in]: 100456
  [out]: one hundred thousand, four hundred and fifty-six
  
  [in]: 10123124001
  [out]: ten billion, one hundred and twenty-three million, one hundred and twenty-four thousand, one 
  </code></pre>
  
  <p>but it breaks at some point:</p>
  
  <pre><code>[in]: 10000000001
  [out]: ten billion, , , one 
  </code></pre>
  
  <p>I've wrote my own version but it involves lots of rules and it caps at one billion, from <a href="http://pastebin.com/WwFCjYtt" rel="nofollow">http://pastebin.com/WwFCjYtt</a>:</p>
  
  <pre><code>import codecs
  
  def num2word (num):
    ones = {1:"one",2:"two",3:"three",4:"four",
            5:"five",6:"six",7:"seven",8:"eight",
            9:"nine",0:"zero",10:"ten"}
    teens = {11:"eleven",12:"twelve",13:"thirteen",
             14:"fourteen",15:"fifteen"}
    tens = {2:"twenty",3:"thirty",4:"forty",
            5:"fifty",6:"sixty",7:"seventy",
            8:"eighty",9:"ninety"}
    lens = {3:"hundred",4:"thousand",6:"hundred",7:"million",
            8:"million", 9:"million",10:"billion"#,13:"trillion",11:"googol",
            }
  
    if num &gt; 999999999:
      return "Number more than 1 billion"
  
    # Ones
    if num &lt; 11:
      return ones[num]
    # Teens
    if num &lt; 20:
      word = ones[num%10] + "teen" if num &gt; 15 else teens[num]
      return word
    # Tens
    if num &gt; 19 and num &lt; 100:
      word = tens[int(str(num)[0])]
      if str(num)[1] == "0":
        return word
      else:
        word = word + " " + ones[num%10]
        return word
  
    # First digit for thousands,hundred-thousands.
    if len(str(num)) in lens and len(str(num)) != 3:
      word = ones[int(str(num)[0])] + " " + lens[len(str(num))]
    else:
      word = ""
  
    # Hundred to Million  
    if num &lt; 1000000:
      # First and Second digit for ten thousands.  
      if len(str(num)) == 5:
        word = num2word(int(str(num)[0:2])) + " thousand"
      # How many hundred-thousand(s).
      if len(str(num)) == 6:
        word = word + " " + num2word(int(str(num)[1:3])) + \
              " " + lens[len(str(num))-2]
      # How many hundred(s)?
      thousand_pt = len(str(num)) - 3
      word = word + " " + ones[int(str(num)[thousand_pt])] + \
              " " + lens[len(str(num))-thousand_pt]
      # Last 2 digits.
      last2 = num2word(int(str(num)[-2:]))
      if last2 != "zero":
        word = word + " and " + last2
      word = word.replace(" zero hundred","")
      return word.strip()
  
    left, right = '',''  
    # Less than 1 million.
    if num &lt; 100000000:
      left = num2word(int(str(num)[:-6])) + " " + lens[len(str(num))]
      right = num2word(int(str(num)[-6:]))
    # From 1 million to 1 billion.
    if num &gt; 100000000 and num &lt; 1000000000:
      left = num2word(int(str(num)[:3])) +  " " + lens[len(str(num))]
      right = num2word(int(str(num)[-6:]))
    if int(str(num)[-6:]) &lt; 100:
      word = left + " and " + right
    else:  
      word = left + " " + right
    word = word.replace(" zero hundred","").replace(" zero thousand"," thousand")
    return word
  
  print num2word(int(raw_input("Give me a number:\n")))
  </code></pre>
  
  <p><strong>How can I make the script i've wrote accept <code>&gt; billion</code>?</strong></p>
  
  <p><strong>Is there any other way to get the same output?</strong></p>
  
  <p><strong>Can my code be written in a less verbose way?</strong></p>