Converting the Double number into words using VB.net


This code converts the Double number into words first it takes the integer value and converts it into words and then the decimal part and converts into words. this code includes arrays, structures and functions using VB code

Declaration of the Structure to store the number and its word



Public Structure tclth
Dim No As Long
Dim Name As String
End Structure



Global declaration of the array variables used in the function


Dim a1(19) As String
Dim a2(8) As String
Dim a3(3) As tclth



Storing the intial values of the numbers correspoing to the array index

Sub InitNumbers()
a1(0) = ""
a1(1) = "One"
a1(2) = "Two"
a1(3) = "Three"
a1(4) = "Four"
a1(5) = "Five"
a1(6) = "Six"
a1(7) = "Seven"
a1(8) = "Eight"
a1(9) = "Nine"
a1(10) = "Ten"
a1(11) = "Eleven"
a1(12) = "Twelve"
a1(13) = "Thirteen"
a1(14) = "Fourteen"
a1(15) = "Fifteen"
a1(16) = "Sixteen"
a1(17) = "Seventeen"
a1(18) = "Eighteen"
a1(19) = "Nineteen"
a2(0) = ""
a2(1) = "Twenty"
a2(2) = "Thirty"
a2(3) = "Forty"
a2(4) = "Fifty"
a2(5) = "Sixty"
a2(6) = "Seventy"
a2(7) = "Eighty"
a2(8) = "Ninty"
a3(0).No = 10000000
a3(0).Name = " Crore "
a3(1).No = 100000
a3(1).Name = " Lakh "
a3(2).No = 1000
a3(2).Name = " Thousand "
a3(3).No = 100
a3(3).Name = " Hundred "
End Sub


Function which is called to convert the number to words which inturn calls other function


Function RupeesNPaise(ByVal Num) As String
RupeesNPaise = Convert2Words(Num)
If GetDecimal(Num) > 0 Then
RupeesNPaise = RupeesNPaise & " and " & Convert2Words(GetDecimal(Num))
Else
'RupeesNPaise = RupeesNPaise & " Only"
RupeesNPaise = RupeesNPaise
End If
End Function





Function Convert2Words(ByVal Num) As String
Call InitNumbers()
Dim a As Long
Dim b As Integer
Dim i As Integer
If Num = 0 Then
Convert2Words = ""
Exit Function
End If
a = Int(Num)
For i = 0 To 3 Step 1
b = a \ a3(i).No
If b > 0 Then
Convert2Words = Convert2Words & InWords(b) & a3(i).Name
End If
a = a - (b * a3(i).No)
Next i
Convert2Words = Convert2Words & InWords(a)
End Function




Function InWords(ByVal No) As String
Dim b As Integer
Dim c As Integer
If No > 19 Then
b = No \ 10
c = No - (b * 10)
InWords = a2(b - 1) & " " & a1(c)
Else
InWords = a1(No)
End If
End Function



For taking the decimal part from the given number



Function GetDecimal(ByVal Number As Double) As Integer
GetDecimal = Val(Mid(Format(Number, "#######.000"), InStr(Format(Number, "#######0.000"), ".") + 1))
End Function


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: