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 (155 Posts)


11 Responses to Convert Numbers to Words in VB.NET

  1. smita says:

    Thanx, it hepled a lot…

  2. [...] the original post: Convert Numbers to Words in VB.NET 8 August 2011 | Uncategorized | Trackback | del.icio.us | Stumble it! | View Count : 0 Next [...]

  3. Sibylle says:

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

  4. gourav says:

    thanks for amazing article it gives lot of useful information…

  5. [...] Convert Numbers to Words in VB.NET [...]

  6. Rakhi says:

    thanks for code

  7. munib says:

    thanks man I need it a lot

  8. mahendra says:

    thanks bro.
    its amazing…

  9. Ras says:

    Hey I am getting error at
    ArrWordList

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.