Numbers to Text

Jynx

Active member
Joined
Oct 20, 2007
Messages
44
Programming Experience
Beginner
Basically I have written an application where on one form you enter an amount such as "125". This then, through my module writes it as in a label as "One Hundred Twenty Five".

Here is the module :

VB.NET:
Module Module1
    Dim Character As String
    Public Class NumtoWords
        Public Function number(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
                number = 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 ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 9), 2), "")
            ElseIf Microsoft.VisualBasic.Len(IM) = 8 Then
                number = 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 ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 8), 2), "")
            ElseIf Microsoft.VisualBasic.Len(IM) = 7 Then
                number = 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 ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 7), 2), "")
            ElseIf Microsoft.VisualBasic.Len(IM) = 6 Then
                number = 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 ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 6), 2), "")
            ElseIf Microsoft.VisualBasic.Len(IM) = 5 Then
                number = conv(Microsoft.VisualBasic.Left(IM, 2), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 1), " Hundred ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 5), 2), " ")
            ElseIf Microsoft.VisualBasic.Len(IM) = 4 Then
                number = conv(Microsoft.VisualBasic.Left(IM, 1), " Thousand ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 2), 1), " Hundred ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 4), 2), " ")
            ElseIf Microsoft.VisualBasic.Len(IM) = 3 Then
                number = conv(Microsoft.VisualBasic.Left(IM, 1), " Hundred ") & conv(Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(IM, 3), 2), "")
            ElseIf Microsoft.VisualBasic.Len(IM) <= 2 Then
                number = conv(Microsoft.VisualBasic.Left(IM, Microsoft.VisualBasic.Len(IM)), "")
            End If
        End Function

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

                If NBR = 3 Then
                    Character = "Thirty"
                    NBR = Right(co, 1)
                    number(NBR, STR)
                    GoTo 10
                End If

                If NBR = 4 Then
                    Character = "Fourty"
                    NBR = Right(co, 1)
                    number(NBR, STR)
                    GoTo 10
                End If

                If NBR = 5 Then
                    Character = "Fifty"
                    NBR = Right(co, 1)
                    number(NBR, STR)
                    GoTo 10
                End If

                If NBR = 6 Then
                    Character = "Sixty"
                    NBR = Right(co, 1)
                    number(NBR, STR)
                    GoTo 10
                End If

                If NBR = 7 Then
                    Character = "Sevnety"
                    NBR = Right(co, 1)
                    number(NBR, STR)
                    GoTo 10
                End If

                If NBR = 8 Then
                    Character = "Eighty"
                    NBR = Right(co, 1)
                    number(NBR, STR)
                    GoTo 10
                End If

                If NBR = 9 Then
                    Character = "Ninty"
                    NBR = Right(co, 1)
                    number(NBR, STR)
                    GoTo 10
                End If
            End If
10:         conv = Character
        End Function
        Private Sub number(ByVal NBR As Integer, ByVal STR As String)
            If NBR = 1 Then Character = Character & " One" & STR
            If NBR = 2 Then Character = Character & " Two" & STR
            If NBR = 3 Then Character = Character & " Three" & STR
            If NBR = 4 Then Character = Character & " Four" & STR
            If NBR = 5 Then Character = Character & " Five" & STR
            If NBR = 6 Then Character = Character & " Six" & STR
            If NBR = 7 Then Character = Character & " Seven" & STR
            If NBR = 8 Then Character = Character & " Eight" & STR
            If NBR = 9 Then Character = Character & " Nine" & STR
        End Sub
    End Class
End Module

Now I'm trying to integrate something new.

I need to make it so that if someone enters "125.25"

It would write the ".25" out like a check.

So it would say:

One Hundred Twenty Five 25/100

Or if only "125" then

One Hundred Twenty Five 00/100


Update :

The more I think about it, the more I can logically see how it has to be done but not sure how to do it.
I figure on form1 when I enter 125.25 in the textbox, I'm going to have to strip away the .25 and only sent the 125 to the module for text conversion. Then carry over the .25 to form2 where it is displayed as "25/100"
Any help is greatly appreciated, you guys always come through for me!
 
Last edited by a moderator:
Having trouble implementing that here

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim totalamtconv As Decimal
totalamtconv = TxtTotalAmt.Text
Dim CLS As New NumtoWords

Form2.checkword.Text = ""
Form2.checkword.Text = CLS.number(TxtTotalAmt.Text)
Form2.checkword.Text = Form2.checkword.Text + " Only"


Form2.datelabel.Text = datebox.Text
Form2.amountlabel.Text = totalamtconv.ToString("C2")
Form2.paytolabel.Text = paytotext.Text
Me.Hide()
Form2.Show()
End Sub
 
Split the string on the decimal point. If the resulting array has one element then there was no decimal point. If it has two then the first element is the dollars and the second is the cents. Have you read the documentation for the String.Split method?
 
Back
Top