ARTICLE

Numeric Value Convert to Word format in VB.NET

Posted by Sapna Malik Articles | Visual Basic 2010 July 26, 2010
In this article, We can see how to convert numerical value to word format in VB.NET.
Download Files:
 
Reader Level:

In this article, We can see how to convert numerical value to word format in vb.net.

This is the .aspx code:-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title></title>

</
head>
<
body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="4" cellspacing="4" width="50%" align="center">
            <tr>
                <td>
                    <asp:Label ID="LableEnterValue" runat="server" ForeColor="Red" Text="Enter Your
Numeric Value"

                        Width="216px"></asp:Label><br />
                    <asp:TextBox ID="NumericTextBox" runat="server" Width="300px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Convert" Width="206px"
OnClick="Button1_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="LabelConvertedValue" runat="server" ForeColor="Red" Text="Your
Converted Value"></
asp:Label><br />
                    <asp:TextBox ID="NumericToWordTextBox" runat="server" Width="500px"
</
asp:TextBox>
                </td>
            </tr>
        </table>
    </div>
    </form>

</
body>
</
html> 

This is the .vb code:-

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim unitNum(20) As String
    Dim hundredNum(20) As String
    Dim thousendNum(20) As String
    Dim myNum As String
    Dim numGroup As Integer

-----------------------------------------------------------------------------

    Public Function NumericToWords(ByVal myNum As String) As String
        Dim units As String
        Dim numericToWordsOutPut As String
        units = numericToWordsOutPut = ""
        Dim myNumArray(myNum.Length \ numGroup) As String
        Dim index As Int16 = -1
        For Count As Int16 = 0 To myNum.Length - numGroup Step numGroup
            index += 1
            myNumArray(index) = myNum.Substring(Count, numGroup)
        Next
        For Count As Int16 = 0 To (myNum.Length \ 3) - 1
            units = thousendNum(((myNum.Length \ numGroup) - 1) - Count + 1)
            numericToWordsOutPut &= CheckUnitOutput(myNumArray(Count)).TrimEnd(" ") & " " & units &
" "
        Next
        myNumArray = Nothing
        Return numericToWordsOutPut
    End Function

-----------------------------------------------------------------------------

    Private Function CheckUnitOutput(ByVal myNum As String) As String
        Dim unitBasedOutput As String = ""
        Dim unitDigit As Int16 = Val(myNum.Substring(0, 1))
        Dim hundredDigit As Int16 = Val(myNum.Substring(1, 1))
        Dim thousendDigit As Int16 = Val(myNum.Substring(2, 1))
        If unitDigit >= 1 Then            unitBasedOutput &= unitNum(unitDigit) & " hundred "
        End If
        If Val(myNum.Substring(1, 2)) < 20 Then
            unitBasedOutput &= unitNum(Val(myNum.Substring(1, 2)))
        End If
        If Val(myNum.Substring(1, 2)) >= 20 Then
            unitBasedOutput &= hundredNum(hundredDigit) & " " & unitNum(thousendDigit)
        End If      
        Return unitBasedOutput
    End Function

------------------------------------------------------------------------------

    Private Sub FormatInitialization()
        thousendNum(0) = ""
        thousendNum(1) = ""
        thousendNum(2) = "Thousand"
        thousendNum(3) = "Million"
        thousendNum(4) = "Billion"
        thousendNum(5) = "Trillion"
        thousendNum(6) = "Quadrillion"
        thousendNum(7) = "Quintillion"
        thousendNum(8) = "Sextillion"
        thousendNum(9) = "Septillion"
        thousendNum(10) = "Octillion"
        thousendNum(11) = "Nonillion"
        thousendNum(12) = "Decillion"
        thousendNum(13) = "Undecillion"
        thousendNum(14) = "Duodecillion"
        thousendNum(15) = "Tredecillion"
        hundredNum(0) = ""
        hundredNum(1) = ""
        hundredNum(2) = "Twenty"
        hundredNum(3) = "Thirty"
        hundredNum(4) = "Forty"
        hundredNum(5) = "Fifty"
        hundredNum(6) = "Sixty"
        hundredNum(7) = "Seventy"
        hundredNum(8) = "Eighty"
        hundredNum(9) = "Ninety"
        unitNum(0) = ""
        unitNum(1) = "One"
        unitNum(2) = "Two"
        unitNum(3) = "Three"
        unitNum(4) = "Four"
        unitNum(5) = "Five"
        unitNum(6) = "Six"
        unitNum(7) = "Seven"
        unitNum(8) = "Eight"
        unitNum(9) = "Nine"
        unitNum(10) = "Ten"
        unitNum(11) = "Eleven"
        unitNum(12) = "Twelve"
        unitNum(13) = "Thirteen"
        unitNum(14) = "Fourteen"
        unitNum(15) = "Fifteen"
        unitNum(16) = "Sixteen"
        unitNum(17) = "Seventeen"
        unitNum(18) = "Eighteen"
        unitNum(19) = "Nineteen"
    End Sub

------------------------------------------------------------------------------

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        numGroup = 3
        FormatInitialization()
    End Sub

------------------------------------------------------------------------------

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        myNum = NumericTextBox.Text
        If myNum.Length Mod numGroup <> 0 Then
            myNum = myNum.PadLeft(myNum.Length + (numGroup - (myNum.Length Mod numGroup)), "0")
        End If
        NumericToWordTextBox.Text = ""
        NumericToWordTextBox.Text = NumericToWords(myNum)
    End Sub

------------------------------------------------------------------------------

End
Class

 Output:-

untitled1.bmp


Login to add your contents and source code to this article
share this article :
post comment
 

Dear Sapna, Thanking you verymuch for best coding and can you kindly send me the codes to do the same in Visual Basic 2008 form or print report to apply for Billing porpose. Awiting your soonest reply. Mohamed Miquash - Sri Lanka.

Posted by Mohamed Miquash Jan 05, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor