-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_to_word.py
89 lines (70 loc) · 2.32 KB
/
number_to_word.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def numbers_to_words(number):
teen_dict = {
'0' : '',
'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':'Fithteen',
'16':'Sixteen',
'17':'Seventeen',
'18':'Eighteen',
'19':"Nineteen"
}
dec_dict = {
'2': 'Twenty',
'3': 'Thirty',
'4': 'Fourty',
'5': 'Fifty',
'6': 'Sixty',
'7': 'Seventy',
'8': 'Eighty',
'9': 'Ninety',
}
cent_dict = {
'3': 'Hundred',
'4': 'Thousand',
'7': 'Million',
'10': 'Billion',
'13': 'Trillion'
}
digit_str = str(number)
digit_len = len(digit_str)
if digit_len == 1:
return teen_dict[digit_str[0]]
if digit_len == 2:
if number < 20:
return teen_dict[str(number)]
if(digit_str[1]!='0'):
return dec_dict[digit_str[0]] + '-' + teen_dict[digit_str[1]]
return dec_dict[digit_str[0]]
if digit_len == 3:
builder = teen_dict[digit_str[0]] + '-' + cent_dict[str(digit_len)]
x = numbers_to_words(int(digit_str[1] + digit_str[2]))#recursion
if x !='':
builder = builder + " and " + x
return builder
if digit_len == 4:
x = teen_dict[digit_str[0]] + "-" + cent_dict['4'] + " " + numbers_to_words(int(digit_str[1:4]))
return x
if digit_len > 4 :
if str(len(digit_str)) in cent_dict:
builder = numbers_to_words(int(digit_str[:1])) + '-' + cent_dict[str(len(digit_str))]#recursion
builder = builder + ", " + numbers_to_words(int(digit_str[1:]))#recursion
elif str(len(digit_str)-1) in cent_dict:
builder = numbers_to_words(int(digit_str[:2])) + '-' + cent_dict[str(len(digit_str)-1)]#recursion
builder = builder + ", " + numbers_to_words(int(digit_str[2:]))#recursion
else:
builder = numbers_to_words(int(digit_str[:3])) + '-' + cent_dict[str(len(digit_str)-2)]#recursion
builder = builder + ", " + numbers_to_words(int(digit_str[3:]))#recursion
return builder