ARTICLE

Number to word conversion in VB.NET

Posted by Satyapriya Nayak Articles | Windows Forms VB.NET August 22, 2011
In this article we can convert any number to its respective words format. Here in this example we can convert it up to 4 digits only.
Reader Level:

In this article we can convert any number to its respective words format. Here in this example we can convert it up to 4 digits only.

Program

Public Class Form1

    Dim n, i As Integer

    Dim x As String

    Dim a As String

    Dim b As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim a() As String = {"One", "Two", "Three", "four", "Five", _

                                     "Six", "Seven", "Eight", "Nine", "Ten", _

                                     "Eleven", "Twelve", "Thirteen", "fourteen", _

                                     "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen"}

 

        Dim b() As String = {"Twenty", "Thirty", "Fourty", "Fifty", _

                             "sixty", "Seventy", "eighty", "ninty"}

 

        x = ""

        n = InputBox("Enter 4 digit Number Only")

        If (n <= 9999) Then

            If (n > 999 And n <= 9999) Then

                x += a((n \ 1000) - 1) & "Thousand"

                n = n Mod 1000

            End If

            x += " "

            If (n > 99 And n <= 999) Then

                x += a((n \ 100) - 1) & "Hundred"

                n = n Mod 100

            End If

            x += " "

            If (n > 19 And n <= 99) Then

                x += b((n \ 10) - 2)

                n = n Mod 10

            End If

            x += " "

            If (n > 0 And n <= 19) Then

                x += a(n - 1)

            End If

            MsgBox("The Number in words is= " & x)

        Else

            MsgBox("Number is out of range")

        End If

    End Sub

End Class

Output

conversion.gif

Thanks for reading.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor