Sunday, 11 December 2011

Convert Number To Words (Integer Or Decimal)


For this , we need a form and a class.
It will convert 8 digit Number to its equivalent figure.
Form consist of 2 textbox and a button. One textbox for reading input, another for displaying output when Button is clicked.

My program will convert both integer and double values



Number to word Class file

Module modNumbertoWord
    Dim Character As String
    Dim num1, num2 As String
    Dim decpart, intpart, AmtStr As String
    Dim ctr As Integer
    Dim Amt As Double

    Public Class Num2Words

        Public Function split(ByVal AB As Double) As String
            Dim sb1 As New System.Text.StringBuilder
            Dim sb2 As New System.Text.StringBuilder
            Dim str As String
            AmtStr = Convert.ToString(AB)

            ctr = 0
            decpart = "0"
            intpart = "0"
            For Each chnum In AmtStr
                If chnum <> "." And ctr = 0 Then
                    sb1.Append(chnum)
                    intpart = sb1.ToString()
                ElseIf chnum = "." Then
                    ctr = 1
                Else
                    sb2.Append(chnum)
                    decpart = sb2.ToString()
                End If
            Next

            If decpart.Length() = 1 Then
                decpart = decpart + "0"
            End If

            Amt = Convert.ToInt64(intpart)
            split = NUMMM(Amt)

            If decpart = "00" Then
                split = split + " Rupees"
            Else
                str = split + " Rupees and "
                Amt = Convert.ToInt64(decpart)
                split = NUMMM(Amt)
                split = str + split + " Paise"
            End If

        End Function


        Public Function NUMMM(ByVal A As Double) As String
            On Error Resume Next
            Dim MM
            Dim IM
            MM = Microsoft.VisualBasic.Len(A)
            IM = Microsoft.VisualBasic.Left(A, Microsoft.VisualBasic.Len(A))
            If Microsoft.VisualBasic.Len(IM) = 9 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 2), " Crore ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 2), " Lack ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 6), 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 7), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 9), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 8 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 1), " Crore ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 2), " Lack ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 5), 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 6), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 8), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 7 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 2), " Lack ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 5), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 7), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 6 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 1), " Lack ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 6), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 5 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 5), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 4 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 1), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 2), 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) = 3 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, 1), " Hundred ") & " and " & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 2), "")
                If Character = "" Then
                    num1 = StrReverse(NUMMM)
                    num2 = num1.Substring(4)
                    NUMMM = StrReverse(num2)
                End If
            ElseIf Microsoft.VisualBasic.Len(IM) <= 2 Then
                NUMMM = conv(Microsoft.VisualBasic.Left(IM, Microsoft.VisualBasic.Len(IM)), "")
            End If
        End Function

        Public Function conv(ByVal co As Integer, ByVal sSTR As String) As String
            On Error Resume Next
            Dim Am As Integer
            Character = ""
            'If co = 0 Then CharacteR = " Zero"
            If co = 1 Then Character = " One" & sSTR
            If co = 2 Then Character = " Two" & sSTR
            If co = 3 Then Character = " Three" & sSTR
            If co = 4 Then Character = " Four" & sSTR
            If co = 5 Then Character = " Five" & sSTR
            If co = 6 Then Character = " Six" & sSTR
            If co = 7 Then Character = " Seven" & sSTR
            If co = 8 Then Character = " Eight" & sSTR
            If co = 9 Then Character = " Nine" & sSTR
            If co = 10 Then Character = " Ten" & sSTR
            If co = 11 Then Character = " Eleven" & sSTR
            If co = 12 Then Character = " Twelve" & sSTR
            If co = 13 Then Character = " Thirteen" & sSTR
            If co = 14 Then Character = " Fourteen" & sSTR
            If co = 15 Then Character = " Fifteen" & sSTR
            If co = 16 Then Character = " Sixteen" & sSTR
            If co = 17 Then Character = " Seventeen" & sSTR
            If co = 18 Then Character = " Eighteen" & sSTR
            If co = 19 Then Character = " Nineteen" & sSTR
            If Character <> "" Then
                GoTo 10
            End If
            If Microsoft.VisualBasic.Len(CStr(co)) = 2 Then
                Am = Left(co, 1)
                If Am = 2 Then
                    Character = "Twenty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 3 Then
                    Character = "Thirty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 4 Then
                    Character = "Fourty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 5 Then
                    Character = "Fifty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 6 Then
                    Character = "Sixty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 7 Then
                    Character = "Seventy"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 8 Then
                    Character = "Eighty"
                    Am = Right(co, 1)
                    nUMM(Am, sSTR)
                    GoTo 10
                End If

                If Am = 9 Then
                    Character = "Ninty"
                    Am = Right(co, 1)
                    'If Am = 1 Then Character = Character & " One" & sSTR
                    'If Am = 2 Then Character = Character & " Two" & sSTR
                    'If Am = 3 Then Character = Character & " Three" & sSTR
                    'If Am = 4 Then Character = Character & " Four" & sSTR
                    'If Am = 5 Then Character = Character & " Five" & sSTR
                    'If Am = 6 Then Character = Character & " Six" & sSTR
                    'If Am = 7 Then Character = Character & " Seven" & sSTR
                    'If Am = 8 Then Character = Character & " Eight" & sSTR
                    'If Am = 9 Then Character = Character & " Nine" & sSTR
                    nUMM(Am, sSTR)
                    GoTo 10
                End If
            End If
10:         conv = Character
        End Function

        Private Sub nUMM(ByVal AM As Integer, ByVal sSTR As String)
            If AM = 1 Then Character = Character & " One" & sSTR
            If AM = 2 Then Character = Character & " Two" & sSTR
            If AM = 3 Then Character = Character & " Three" & sSTR
            If AM = 4 Then Character = Character & " Four" & sSTR
            If AM = 5 Then Character = Character & " Five" & sSTR
            If AM = 6 Then Character = Character & " Six" & sSTR
            If AM = 7 Then Character = Character & " Seven" & sSTR
            If AM = 8 Then Character = Character & " Eight" & sSTR
            If AM = 9 Then Character = Character & " Nine" & sSTR
            If AM = 0 Then Character = Character & sSTR
        End Sub
    End Class
End Module

btnClick click event in Form

Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
        Dim CLS As New Num2Words
        txtWord.Text = ""
        'For Displaying Integer Values
        txtWord.Text = CLS.NUMMM(txtNo.Text)
        'For Displaying Double value
        'txtWord.Text = CLS.split(txtNo.Text)
        txtWord.Text = txtWord.Text + " Only"
    End Sub

 
For Displaying Integer Values Type below code
  txtWord.Text = CLS.NUMMM(txtNo.Text)

                  OR

For Displaying Double value Type Below code
  txtWord.Text = CLS.split(txtNo.Text)






No comments:

Post a Comment