Convert Numbers to Words in VB.NET

This project is about How to Convert any Number to Words. You can Convert any Decimal Number to Word using VB.NET. The code allow you to convert the number to the exact Rupees and Paisa. I hope you guys will enjoy this code. In this project we are using a class file to define the methods for converting Numbers to Words.

 




Free Download

Convert Numbers To Words : Demo Project

 




Controls and Properties 

To convert Numbers to Words We are using one Window Form
"Form1" and one class file "Module1".

 

Code Page:

1. Form1.vb page:

Private Sub cmdConvert_Click(ByVal sender As System.Object,
         ByVal e As System.EventArgs) Handles cmdConvert.Click
        Try
            lblInword.Text = AmtInWord(txtNum.Text)
        Catch ex As Exception
            lblInword.Text = ex.ToString
        End Try
    End Sub
2. Module1.vb class file
Module Module1
    Function strReplicate(ByVal str As String, ByVal intD As Integer) As String
        Dim i As Integer
        strReplicate = ""
        For i = 1 To intD
            strReplicate = strReplicate + str
        Next
        Return strReplicate
    End Function
    Function AmtInWord(ByVal Num As Decimal) As String
        Dim strNum As String
        Dim strNumDec As String
        Dim StrWord As String
        strNum = Num

        If InStr(1, strNum, ".") <> 0 Then
            strNumDec = Mid(strNum, InStr(1, strNum, ".") + 1)

            If Len(strNumDec) = 1 Then
                strNumDec = strNumDec + "0"
            End If
            If Len(strNumDec) > 2 Then
                strNumDec = Mid(strNumDec, 1, 2)
            End If

            strNum = Mid(strNum, 1, InStr(1, strNum, ".") - 1)
            StrWord = IIf(CDbl(strNum) = 1, " Rupee ", " Rupees ") + NumToWord(CDbl(strNum))
                     + IIf(CDbl(strNumDec) > 0, " and Paise" + cWord3(CDbl(strNumDec)), "")
        Else
            StrWord = IIf(CDbl(strNum) = 1, " Rupee ", " Rupees ") + NumToWord(CDbl(strNum))
        End If
        AmtInWord = StrWord & " Only"
        Return AmtInWord
    End Function
    Function NumToWord(ByVal Num As Decimal) As String
        Dim strNum As String
        Dim StrWord As String
        strNum = Num

        If Len(strNum) <= 3 Then
            StrWord = cWord3(CDbl(strNum))
        Else
            StrWord = cWordG3(CDbl(Mid(strNum, 1, Len(strNum) - 3))) + " "
                      + cWord3(CDbl(Mid(strNum, Len(strNum) - 2)))
        End If
        NumToWord = StrWord
    End Function
    Function cWordG3(ByVal Num As Decimal) As String
        Dim strNum As String = ""
        Dim StrWord As String = ""
        Dim readNum As String = ""
        strNum = Num
        If Len(strNum) Mod 2 <> 0 Then
            readNum = CDbl(Mid(strNum, 1, 1))
            If readNum <> "0" Then
                StrWord = retWord(readNum)
                readNum = CDbl("1" + strReplicate("0", Len(strNum) - 1) + "000")
                StrWord = StrWord + " " + retWord(readNum)
            End If
            strNum = Mid(strNum, 2)
        End If
        While Not Len(strNum) = 0
            readNum = CDbl(Mid(strNum, 1, 2))
            If readNum <> "0" Then
                StrWord = StrWord + " " + cWord3(readNum)
                readNum = CDbl("1" + strReplicate("0", Len(strNum) - 2) + "000")
                StrWord = StrWord + " " + retWord(readNum)
            End If
            strNum = Mid(strNum, 3)
        End While
        cWordG3 = StrWord
        Return cWordG3
    End Function
    Function cWord3(ByVal Num As Decimal) As String
        Dim strNum As String = ""
        Dim StrWord As String = ""
        Dim readNum As String = ""
        If Num < 0 Then Num = Num * -1
        strNum = Num

        If Len(strNum) = 3 Then
            readNum = CDbl(Mid(strNum, 1, 1))
            StrWord = retWord(readNum) + " Hundred"
            strNum = Mid(strNum, 2, Len(strNum))
        End If

        If Len(strNum) <= 2 Then
            If CDbl(strNum) >= 0 And CDbl(strNum) <= 20 Then
                StrWord = StrWord + " " + retWord(CDbl(strNum))
            Else
                StrWord = StrWord + " " + retWord(CDbl(Mid(strNum, 1, 1) + "0")) + " "
                          + retWord(CDbl(Mid(strNum, 2, 1)))
            End If
        End If

        strNum = CStr(Num)
        cWord3 = StrWord
        Return cWord3
    End Function

    Function retWord(ByVal Num As Decimal) As String
        retWord = ""
        Dim ArrWordList(,) As Object = {{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, "Forty"}, {50, "Fifty"}
                                        , {60, "Sixty"}, _ {70, "Seventy"}, {80, "Eighty"}, {90, "Ninety"}
                                        , {100, "Hundred"}, {1000, "Thousand"}, _ {100000, "Lakh"}
                                        , {10000000, "Crore"}}

        Dim i As Integer
        For i = 0 To UBound(ArrWordList)
            If Num = ArrWordList(i, 0) Then
                retWord = ArrWordList(i, 1)
                Exit For
            End If
        Next
        Return retWord
    End Function
End Module
admin (158 Posts)


Comments ( 11 )

  1. smitaAugust 8, 2011 15 // Reply

    Thanx, it hepled a lot...

  2. SibylleAugust 11, 2011 16 // Reply

    Reached your website through Yahoo. You know I am signing up to your feed.

  3. gouravAugust 12, 2011 16 // Reply

    thanks for amazing article it gives lot of useful information...

  4. sumitNovember 28, 2011 18 // Reply

    Thanks,

  5. RakhiMay 29, 2012 13 // Reply

    thanks for code

  6. munibOctober 22, 2012 17 // Reply

    thanks man I need it a lot

  7. mahendraJanuary 31, 2013 17 // Reply

    thanks bro. its amazing...

  8. RasAugust 14, 2013 16 // Reply

    Hey I am getting error at ArrWordList

Leave a reply

Your email address will not be published.

Comment moderation is enabled. Your comment may take some time to appear.