-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_in_words.py
More file actions
21 lines (17 loc) · 810 Bytes
/
number_in_words.py
File metadata and controls
21 lines (17 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def number_to_words(n):
to19 = 'One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen'.split()
tens = 'Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety'.split()
thousand_and_up = ['Thousand', 'Million', 'Billion']
def words(num):
if num < 20:
return to19[num-1:num]
if num < 100:
return [tens[num/10-2]] + words(num%10)
if num < 1000:
return [to19[num/100-1]] + ['Hundred'] + words(num%100)
for p, w in enumerate(thousand_and_up, 1):
if num < 1000**(p+1):
return words(num/1000**p) + [w] + words(num%1000**p)
return ' '.join(words(n)) or "Zero"
if __name__ == '__main__':
print number_to_words(1129832261)