-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnum2text.py
349 lines (308 loc) · 10.6 KB
/
num2text.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright 2019 Utkarsh Yadav
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This program has limitations as it can only translate to an extent
# it can translate upto (10^64 -1)
def num2text(dat, delimiter=", "):
# input inspection
if (dat > pow(10, 64) - 1):
return "Input Out of range"
elif (dat < 0):
print("Invalid Input \n converting to positive number")
dat = dat * (-1)
# default declaration in case they are not used and are needed to print
quintillion = ""
quadrillion = ""
sextillion = ""
septillion = ""
octillion = ""
nonillion = ""
decillion = ""
undecillion = ""
duodecillion = ""
tredecillion = ""
quattuordecillion = ""
quindecillion = ""
sexdecillion = ""
septemdecillion = ""
octodecillion = ""
novemdecillion = ""
vigintillion = ""
billions = ""
trillions = ""
millions = ""
thousands = ""
hundreds = ""
tens = ""
units = ""
nums = ""
# checks weather the number lies between 10 or 20 to assign special names
if (dat % 100 > 10 and dat % 100 < 20):
nums = toteens(dat)
dat = dat - (dat % 100)
else: # gives normal names to upto two digit numbers excluding 11 12 13 14 15 16 17 18 19
units = tounits(dat)
dat = dat - (dat % 10)
tens = totens(dat)
dat = dat - (dat % 100)
# general form of naming for numbers greater than 99 this function uses itself for comples repedted naming
if (dat > 99):
if (((dat % 1000) / 100) != 0):
hundreds = num2text((dat % 1000) / 100) + " Hundred "
dat = dat - (dat % 1000)
if (dat > 999):
if (((dat % 1000000) / 1000) != 0):
thousands = num2text((dat % 1000000) / 1000) + \
" Thousand" + delimiter
dat = dat - (dat % 1000000)
if (dat > 999999):
if (((dat % 10**9) / 10**6) != 0):
millions = num2text(
(dat % 10**9) / 10**6) + " Million" + delimiter
dat = dat - (dat % 10**9)
if (dat > 999999999):
if (((dat % 10**12) / 10**9) != 0):
billions = num2text(
(dat % 10**12) / 10**9) + " Billion" + delimiter
dat = dat - (dat % 10**12)
if (dat > 999999999999):
if (((dat % 10**15) / 10**12) != 0):
trillions = num2text((dat % 10**15) /
10**12) + " Trillion" + delimiter
dat = dat - (dat % 10**15)
if (dat > 999999999999999):
if (((dat % 10**18) / 10**15) != 0):
quadrillion = num2text(
(dat % 10**18) /
10**15) + " Quadrillion" + delimiter
dat = dat - (dat % 10**18)
if (dat > 999999999999999999):
if (((dat % 10**21) / 10**18) != 0):
quintillion = num2text(
(dat % 10**21) /
10**18) + " Quintillion" + delimiter
dat = dat - (dat % 10**21)
if (dat > 999999999999999999999):
if (((dat % 10**24) / 10**21) != 0):
sextillion = num2text(
(dat % 10**24) /
10**21) + " Sextillion" + delimiter
dat = dat - (dat % 10**24)
if (dat > 999999999999999999999999):
if (((dat % 10**27) / 10**24)
!= 0):
septillion = num2text(
(dat % 10**27) /
10**24) + " Septillion" + delimiter
dat = dat - (dat % 10**27)
if (dat > 999999999999999999999999999):
if (((dat % 10**30) /
10**27) != 0):
octillion = num2text(
(dat % 10**30) /
10**27) + " Octillion" + delimiter
dat = dat - (dat % 10**30)
if (dat > 999999999999999999999999999999):
if (((dat % 10*33) /
10**30) != 0):
nonillion = num2text(
(dat % 10**33) /
10**30) + " Nonillion" + delimiter
dat = dat - (dat % 10**33)
if (dat > 999999999999999999999999999999999):
if (((dat % 10**36) /
10**33) != 0):
decillion = num2text(
(dat % 10**36) /
10**33) + " Decillion" + delimiter
dat = dat - (dat % 10**36)
if (dat > 999999999999999999999999999999999999):
if (((dat % 10**39) /
10**36) != 0):
undecillion = num2text(
(dat % 10**39) /
10**36
) + " Undecillion" + delimiter
dat = dat - (dat % 10**39)
if (dat > 999999999999999999999999999999999999999):
if (((dat % 10**42) /
10**39) != 0):
duodecillion = num2text(
(dat % 10**42) /
10**39
) + " Duodecillion" + delimiter
dat = dat - (dat % 10**42)
if (dat > 999999999999999999999999999999999999999999):
if (((dat % 10**45) /
10**42) != 0):
tredecillion = num2text(
(dat % 10**45) /
10**42
) + " Tredecillion" + delimiter
dat = dat - (dat % 10**45)
if (dat > 999999999999999999999999999999999999999999999):
if (((dat % 10**48) /
10**45) != 0):
quattuordecillion = num2text(
(dat % 10**48) /
10**45
) + " Quattuordecillion" + delimiter
dat = dat - (dat % 10**48)
if (dat > 999999999999999999999999999999999999999999999999):
if (((dat % 10**51) /
10**48) != 0):
quindecillion = num2text(
(dat % 10**51) /
10**48
) + " Quindecillion" + delimiter
dat = dat - (dat % 10**51)
if (dat > 999999999999999999999999999999999999999999999999999):
if (((dat % 10**54) /
10**51) != 0):
sexdecillion = num2text(
(dat % 10**54) /
10**51
) + " Sexdecillion" + delimiter
dat = dat - (dat %
10**54)
if (dat > 999999999999999999999999999999999999999999999999999999):
if (((dat % 10**57) /
10**54) != 0):
septemdecillion = num2text(
(dat % 10**57)
/ 10**54
) + " Septemdecillion" + delimiter
dat = dat - (dat %
10**57)
if (dat > 999999999999999999999999999999999999999999999999999999999):
if (((dat % 10**60)
/ 10**57) != 0):
octodecillion = num2text(
(dat %
10**60) /
10**57
) + " Octodecillion" + delimiter
dat = dat - (
dat % 10**60)
if (dat > 999999999999999999999999999999999999999999999999999999999999):
if (((dat %
10**63) /
10**60) != 0):
novemdecillion = num2text(
(dat %
10**63)
/ 10**60
) + " Novemdecillion" + delimiter
dat = dat - \
(dat % 10**63)
if (dat > 999999999999999999999999999999999999999999999999999999999999999):
if (((dat %
10**66)
/ 10**63)
!= 0):
vigintillion = num2text((
dat %
10**66
) / 10**63
) + " Vigintillion" + delimiter
dat = dat - \
(dat % 10**66)
# returns the name to either the function itself or the user
num_name = (vigintillion + novemdecillion + octodecillion +
septemdecillion + sexdecillion + quindecillion +
quattuordecillion + tredecillion + duodecillion + undecillion +
decillion + nonillion + octillion + septillion + sextillion +
quintillion + quadrillion + trillions + billions + millions +
thousands + hundreds + tens + units + nums)
return num_name.strip(", ")
# this function is used to give special names to number between 10 and 20
def toteens(dat):
dat = dat % 100
if (dat == 11.0 or dat == 11):
num = "Eleven"
elif (dat == 12.0 or dat == 12):
num = "Twelve"
elif (dat == 13.0 or dat == 13):
num = "Thirteen"
elif (dat == 14.0 or dat == 14):
num = "Fourteen"
elif (dat == 15.0 or dat == 15):
num = "Fifteen"
elif (dat == 16.0 or dat == 16):
num = "Sixteen"
elif (dat == 17.0 or dat == 17):
num = "Seventeen"
elif (dat == 18.0 or dat == 18):
num = "Eighteen"
elif (dat == 19.0 or dat == 19):
num = "Nineteen"
return (num)
# general purpose naming of number at unit place is completed hear
def tounits(dat):
pof = ""
unit = dat % 10
if (unit == 1.0 or unit == 1):
pof = "One"
elif (unit == 2.0 or unit == 2):
pof = "Two"
elif (unit == 3.0 or unit == 3):
pof = "Three"
elif (unit == 4.0 or unit == 4):
pof = "Four"
elif (unit == 5.0 or unit == 5):
pof = "Five"
elif (unit == 6.0 or unit == 6):
pof = "Six"
elif (unit == 7.0 or unit == 7):
pof = "Seven"
elif (unit == 8.0 or unit == 8):
pof = "Eight"
elif (unit == 9.0 or unit == 9):
pof = "Nine"
elif (dat == 0.0 or dat == 0):
pof = "Zero"
return (pof)
# general purpose naming of numbers at tens place is completed hear
def totens(dat):
pof = ""
unit = dat % 100
if (unit == 10):
pof = "Ten"
elif (unit == 20):
pof = "Twenty "
elif (unit == 30):
pof = "Thirty "
elif (unit == 40):
pof = "Fourty "
elif (unit == 50):
pof = "Fifty "
elif (unit == 60):
pof = "Sixty "
elif (unit == 70):
pof = "Seventy "
elif (unit == 80):
pof = "Eighty "
elif (unit == 90):
pof = "Ninety "
return (pof)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--delimiter", help="choose the delimiter")
parser.add_argument("Number", type=int)
args = parser.parse_args()
if args.Number:
if args.delimiter:
print(num2text(args.Number, delimiter=args.delimiter))
else:
print(num2text(args.Number))