Thursday, September 9, 2010

Number to Text in VB6 Indian Style

Dim valforunit(9) As String
Dim valforhun(9) As String
Dim valforten(9) As String
Dim allval As String
Public Function Text_to_Word(PNum As Double) As String
valforunit(1) = "One"
valforunit(2) = "Two"
valforunit(3) = "Three"
valforunit(4) = "Four"
valforunit(5) = "Five"
valforunit(6) = "Six"
valforunit(7) = "Seven"
valforunit(8) = "Eight"
valforunit(9) = "Nine"

valforten(1) = "Eleven"
valforten(2) = "Twelve"
valforten(3) = "Thirteen"
valforten(4) = "Fourteen"
valforten(5) = "Fiveteen"
valforten(6) = "Sixteen"
valforten(7) = "Seventeen"
valforten(8) = "Eighteen"
valforten(9) = "Nineteen"
valforten(0) = "Ten"

valforhun(1) = "Ten"
valforhun(2) = "Twenty"
valforhun(3) = "Thirty"
valforhun(4) = "Forty"
valforhun(5) = "Fifty"
valforhun(6) = "Sixty"
valforhun(7) = "Seventy"
valforhun(8) = "Eighty"
valforhun(9) = "Ninety"
allval = ""
Dim sanstr As Variant
X = PNum
i = 2
If Len(X) > 2 Then
a = Len(X) / 2
a = CStr(a)
b = InStr(1, a, ".", 1)
If b <> -1 And b <> 0 Then a = Mid(a, 1, b)
a = CInt(a) + 1
Else
a = 1
End If
For k = 1 To a
If k = 1 Then
sanstr = Right(X, 2)
ElseIf k = 2 Then
sanstr = Left(Right(X, 3), 1)
Else
sanstr = Right(X, k + i)
m = Len(sanstr) Mod 2
If m = 1 Then
sanstr = Left(sanstr, 2)
Else
sanstr = Left(sanstr, 1)
End If
i = i + 1
End If
Call sanfun(sanstr)
If k = 2 And sanstr <> "" Then
sanstr = sanstr & " Hundred "
ElseIf k = 3 And sanstr <> "" And sanstr <> " " Then
sanstr = sanstr & " Thousand "
ElseIf k = 4 And sanstr <> "" Then
If sanstr = "One" Then
sanstr = sanstr & " Lakh "
Else
sanstr = sanstr & " Lakhs "
End If
ElseIf k = 5 And sanstr <> "" Then
If sanstr = "One" Then
sanstr = sanstr & " Crore "
Else
sanstr = sanstr & " Crores "
End If
End If
allval = sanstr & allval

Next
If Trim(Right(allval, 5)) = "And" Then
allval = Trim(Replace(Trim(allval), "And", ""))
End If
Text_to_Word = allval
End Function
Private Sub sanfun(ByRef X As Variant)
Dim arr() As String
ReDim arr(Len(X))
For j = 0 To Len(X) - 1
a = Right(X, j + 1)
a = Left(a, 1)
arr(j) = a
Next
asize = UBound(arr)
If asize = 1 Then
X = valforunit(arr(0))
ElseIf asize = 2 Then
If paise = 1 Then
X = valforunit(arr(1)) & " " & valforunit(arr(0))
Exit Sub
End If
If arr(1) = 1 Then
X = valforten(arr(0))
Else
X = valforhun(arr(1)) & " " & valforunit(arr(0))
End If
End If
End Sub

Private Sub Command1_Click()
MsgBox Text_to_Word(Text1.Text)
End Sub

No comments:

Post a Comment