• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Numbernator - From integers to words. alvations  Nov 10th, 2012 848 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import codecs
2.
3. def num2word (num):
4.   ones = {1:"one",2:"two",3:"three",4:"four",
5.           5:"five",6:"six",7:"seven",8:"eight",
6.           9:"nine",0:"zero",10:"ten"}
7.   teens = {11:"eleven",12:"twelve",13:"thirteen",
8.            14:"fourteen",15:"fifteen"}
9.   tens = {2:"twenty",3:"thirty",4:"forty",
10.           5:"fifty",6:"sixty",7:"seventy",
11.           8:"eighty",9:"ninety"}
12.   lens = {3:"hundred",4:"thousand",6:"hundred",7:"million",
13.           8:"million", 9:"million",10:"billion"#,13:"trillion",11:"googol",
14.           }
15.
16.   if num > 999999999:
17.     return "Number more than 1 billion"
18.
19.   # Ones
20.   if num < 11:
21.     return ones[num]
22.   # Teens
23.   if num < 20:
24.     word = ones[num%10] + "teen" if num > 15 else teens[num]
25.     return word
26.   # Tens
27.   if num > 19 and num < 100:
28.     word = tens[int(str(num))]
29.     if str(num) == "0":
30.       return word
31.     else:
32.       word = word + " " + ones[num%10]
33.       return word
34.
35.   # First digit for thousands,hundred-thousands.
36.   if len(str(num)) in lens and len(str(num)) != 3:
37.     word = ones[int(str(num))] + " " + lens[len(str(num))]
38.   else:
39.     word = ""
40.
41.   # Hundred to Million
42.   if num < 1000000:
43.     # First and Second digit for ten thousands.
44.     if len(str(num)) == 5:
45.       word = num2word(int(str(num)[0:2])) + " thousand"
46.     # How many hundred-thousand(s).
47.     if len(str(num)) == 6:
48.       word = word + " " + num2word(int(str(num)[1:3])) + \
49.             " " + lens[len(str(num))-2]
50.     # How many hundred(s)?
51.     thousand_pt = len(str(num)) - 3
52.     word = word + " " + ones[int(str(num)[thousand_pt])] + \
53.             " " + lens[len(str(num))-thousand_pt]
54.     # Last 2 digits.
55.     last2 = num2word(int(str(num)[-2:]))
56.     if last2 != "zero":
57.       word = word + " and " + last2
58.     word = word.replace(" zero hundred","")
59.     return word.strip()
60.
61.   left, right = '',''
62.   # Less than 1 million.
63.   if num < 100000000:
64.     left = num2word(int(str(num)[:-6])) + " " + lens[len(str(num))]
65.     right = num2word(int(str(num)[-6:]))
66.   # From 1 million to 1 billion.
67.   if num > 100000000 and num < 1000000000:
68.     left = num2word(int(str(num)[:3])) +  " " + lens[len(str(num))]
69.     right = num2word(int(str(num)[-6:]))
70.   if int(str(num)[-6:]) < 100:
71.     word = left + " and " + right
72.   else:
73.     word = left + " " + right
74.   word = word.replace(" zero hundred","").replace(" zero thousand"," thousand")
75.   return word
76.
77. print num2word(int(raw_input("Give me a number:\n")))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top