Convert Whole Number and Decimal to Words in VB.NET

Today, I will teach you how to create a program that converts a whole number and a decimal into words using vb.net. This is so useful in making systems when you convert it in the receipt section that the numbers are converted into words. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add one TextBox named TextBox1 for the input, one Label named Label1 for the output, and one Button named Button1 for converting the input into words. You must design your interface like this: output 3. Now, we will create a Module named Module1 for the Number to Words code. We will create a Function named NumberToWord(ByVal MyNumber) Initialize the following:
  1.  Dim Temp
  2.         Dim Num, Digits As String
  3.         Dim DecimalPlace, iCount
  4.         Dim Hundreds, Words As String
  5.         Dim place(9) As String
  6.         place(0) = " Thousand "
  7.         place(2) = " Hundred "
  8.         place(4) = "  "
  9.         place(6) = "  "
  10.         place(8) = "  "
  11.         On Error Resume Next
Convert MyNumber to a string, trimming extra spaces.
  1.  MyNumber = Trim(Str(MyNumber))
Find decimal place.
  1.  DecimalPlace = InStr(MyNumber, ".")
Find decimal place.
  1.  DecimalPlace = InStr(MyNumber, ".")
If we find decimal place...
  1.  If DecimalPlace > 0 Then
  2.             ' Convert Digits
  3.             Temp = Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2)
  4.             Digits = " and " & ConvertTens(Temp)
  5.  
  6.             ' Strip off Digits from remainder to convert.
  7.             MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
  8.         End If
If MyNumber between 1 To 99 Only.
  1.  Dim TM As String  ' If MyNumber between 1 To 99 Only.
  2.         TM = Right(MyNumber, 2)
  3.  
  4.         If Len(MyNumber) > 0 And Len(MyNumber) <= 2 Then
  5.             If Len(TM) = 1 Then
  6.                 Words = ConvertDigit(TM)
  7.                 NumberToWord = Words & Digits
  8.  
  9.                 Exit Function
  10.  
  11.             Else
  12.                 If Len(TM) = 2 Then
  13.                     Words = ConvertTens(TM)
  14.                     NumberToWord = Words & Digits
  15.                     Exit Function
  16.  
  17.                 End If
  18.             End If
  19.         End If
Convert last 3 digits of MyNumber to number in word.
  1.   Hundreds = ConvertHundreds(Right(MyNumber, 3))
Strip off last three digits
  1.  MyNumber = Left(MyNumber, Len(MyNumber) - 3)
  1.  iCount = 0
  2.         Do While MyNumber <> ""
  3.             'Strip last two digits
  4.             Temp = Right(MyNumber, 2)
  5.             If Len(MyNumber) = 1 Then
  6.  
  7.  
  8.                 If Trim(Words) = "Thousand" Or _
  9.                 Trim(Words) = "Hundred  Thousand" Or _
  10.                 Trim(Words) = "Hundred" Then
  11.  
  12.  
  13.                     Words = ConvertDigit(Temp) & place(iCount)
  14.                     MyNumber = Left(MyNumber, Len(MyNumber) - 1)
  15.  
  16.                 Else
  17.  
  18.                     Words = ConvertDigit(Temp) & place(iCount) & Words
  19.                     MyNumber = Left(MyNumber, Len(MyNumber) - 1)
  20.  
  21.                 End If
  22.             Else
  23.  
  24.                 If Trim(Words) = "Thousand" Or _
  25.                    Trim(Words) = "Hundred  Thousand" Or _
  26.                    Trim(Words) = "Hundred" Then
  27.  
  28.  
  29.                     Words = ConvertTens(Temp) & place(iCount)
  30.  
  31.  
  32.                     MyNumber = Left(MyNumber, Len(MyNumber) - 2)
  33.                 Else
  34.  
  35.                     '=================================================================
  36.  
  37.  
  38.                     If Trim(ConvertTens(Temp) & place(iCount)) = "Hundred" Then
  39.  
  40.                         Words = Words
  41.                         MyNumber = Left(MyNumber, Len(MyNumber) - 2)
  42.                     Else
  43.                         Words = ConvertTens(Temp) & place(iCount) & Words
  44.                         MyNumber = Left(MyNumber, Len(MyNumber) - 2)
  45.                     End If
  46.  
  47.                 End If
  48.             End If
  49.  
  50.             iCount = iCount + 2
  51.         Loop
  52.  
  53.         NumberToWord = " " & Words & Hundreds & Digits & ""
  54.     End Function
4. For the conversion of hundreds, create a function named ConvertHundreds(ByVal MyNumber)
  1.  Private Function ConvertHundreds(ByVal MyNumber)
  2.         Dim Result As String
  3.  
  4.         ' Exit if there is nothing to convert.
  5.         If Val(MyNumber) = 0 Then Exit Function
  6.  
  7.         ' Append leading zeros to number.
  8.         MyNumber = Right("000" & MyNumber, 3)
  9.  
  10.         ' Do we have a hundreds place digit to convert?
  11.         If Left(MyNumber, 1) <> "0" Then
  12.             Result = ConvertDigit(Left(MyNumber, 1)) & " Hundred "
  13.         End If
  14.  
  15.         ' Do we have a tens place digit to convert?
  16.         If Mid(MyNumber, 2, 1) <> "0" Then
  17.             Result = Result & ConvertTens(Mid(MyNumber, 2))
  18.         Else
  19.             ' If not, then convert the ones place digit.
  20.             Result = Result & ConvertDigit(Mid(MyNumber, 3))
  21.         End If
  22.  
  23.         ConvertHundreds = Trim(Result)
  24.     End Function
5. For the conversion of tens, create a function named ConvertTens(ByVal MyTens)
  1.   Private Function ConvertTens(ByVal MyTens)
  2.         Dim Result As String
  3.  
  4.         ' Is value between 10 and 19?
  5.         If Val(Left(MyTens, 1)) = 1 Then
  6.             Select Case Val(MyTens)
  7.                 Case 10 : Result = "Ten"
  8.                 Case 11 : Result = "Eleven"
  9.                 Case 12 : Result = "Twelve"
  10.                 Case 13 : Result = "Thirteen"
  11.                 Case 14 : Result = "Fourteen"
  12.                 Case 15 : Result = "Fifteen"
  13.                 Case 16 : Result = "Sixteen"
  14.                 Case 17 : Result = "Seventeen"
  15.                 Case 18 : Result = "Eighteen"
  16.                 Case 19 : Result = "Nineteen"
  17.                 Case Else
  18.             End Select
  19.         Else
  20.             ' .. otherwise it's between 20 and 99.
  21.             Select Case Val(Left(MyTens, 1))
  22.                 Case 2 : Result = "Twenty "
  23.                 Case 3 : Result = "Thirty "
  24.                 Case 4 : Result = "Forty "
  25.                 Case 5 : Result = "Fifty "
  26.                 Case 6 : Result = "Sixty "
  27.                 Case 7 : Result = "Seventy "
  28.                 Case 8 : Result = "Eighty "
  29.                 Case 9 : Result = "Ninety "
  30.                 Case Else
  31.             End Select
  32.  
  33.             ' Convert ones place digit.
  34.             Result = Result & ConvertDigit(Right(MyTens, 1))
  35.         End If
  36.  
  37.         ConvertTens = Result
  38.     End Function
6. For the conversion of digits, create a function named ConvertDigit(ByVal MyDigit)
  1.  Private Function ConvertDigit(ByVal MyDigit)
  2.         Select Case Val(MyDigit)
  3.             Case 1 : ConvertDigit = "One"
  4.             Case 2 : ConvertDigit = "Two"
  5.             Case 3 : ConvertDigit = "Three"
  6.             Case 4 : ConvertDigit = "Four"
  7.             Case 5 : ConvertDigit = "Five"
  8.             Case 6 : ConvertDigit = "Six"
  9.             Case 7 : ConvertDigit = "Seven"
  10.             Case 8 : ConvertDigit = "Eight"
  11.             Case 9 : ConvertDigit = "Nine"
  12.             Case Else : ConvertDigit = ""
  13.         End Select
  14.     End Function
7. Now, we will code for our Form. We will first filter the inputting of numbers in our TextBox.
  1.     Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  2.         If Not Char.IsDigit(e.KeyChar) AndAlso Not e.KeyChar = "."c Then
  3.             e.Handled = True
  4.             If (Asc(e.KeyChar) = 8) Then
  5.                 e.Handled = False
  6.             End If
  7.         End If
  8.     End Sub
8. Lastly, code for the Form_Load then use the NumberToWord function we have created in our Module.
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         If Val(TextBox1.Text) > 0 Then
  3.             Label1.Text = NumberToWord(Val(TextBox1.Text))
  4.         Else
  5.             MessageBox.Show("Please enter any amount")
  6.         End If
  7.     End Sub

Output:

output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment