How To Create Number To String Converter Using Python language

Posted by Suraj Singh on January 14, 2017 · 8 mins read
hello readers,

                         Today, I Am Showing About How We Can Create Number to String Converter Using Python Language. So, Here I Created Python Script for Converting digits to Indian Rupees.



let's Start

here, it is example.

  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
# Configuration For Numbers
form={
'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':'Fifteen',
'16':'Sixteen',
'17':'Seventeen',
'18':'Eighteen',
'19':'Nineteen',
'20':'Twenty',
'30':'Thirty',
'40':'Fourty',
'50':'Fifty',
'60':'Sixty',
'70':'Seventy',
'80':'Eighty',
'90':'Ninety'
}
# Positional Values
val=[
'',
'',
' Thousand, ',
' Lakh, ',
' Crore, ',
' Million, ',
' Billion, ']

# Creating Class
class Number_Convert:
def __init__(self, num=None):
self.num=num
def convert(self):
raw=self.cutt()
raw1=''
for i in raw.split(','):
raw1=raw1+self.tranl(i)+','

raw1=raw1.split(',')
raw1.reverse()
st=[]
for i in range(len(raw1)):
if len(raw1[i])!=0:
data=str(raw1[i])+str(val[i])
st.append(data)
else:
pass
st.reverse()
data=''
for i in st:
data=data+i
return data

def cutt(self):
data=self.num[::-1]
k=0
l=''
for i in data:
if k%2==1 and k!=1:
l=l+','+i
k=k+1
pass
else:
l=l+i
k=k+1
pass
data=l[::-1]
return data
def tranl(self, i):
data=i
if int(data)==0:
return ''
elif int(data)<=20:
return form[str(int(data))]
elif len(data)==2:
x=data[0]+'0'
y=data[1]
rw=form[x]+'-'+form[y]
return rw
elif len(data)==3:
x=data[0]
y=data[1]+'0'
z=data[2]
if int(x)!=0:
hun=' Hundred And '
else:
hun=' '
rw=form[x]+hun+form[y]+'-'+form[z]
return rw
else:
return 'Sorry! Not Programmed For This'


if __name__=='__main__':
print Number_Convert('12345678').convert()
This Example is Written By surajsinghbisht054@gmail.com

# Output
As You Can See, I Entered "12345678" Input in 108 line and i got this output

One Crore, Twenty-Three Lakh, Fourty-Five Thousand, Six Hundred And Seventy-Eight

So, I Shared This Example Only For Showing Python Example.
if You Wants to Convert digits to another currency (Other Then Indian rupees)
You Can Modify This Python Examples as Your Needs.

Done!
Thanks For Support
Please, Feel Free To Leave A Comment If Our Article has Helped You.
Suraj
surajsinghbisht054@gmail.com