Public Class ConverttoWord
Public Shared Function ConvertNum(ByVal Input As Decimal) As String
Dim formatnumber As String
Dim numparts(10) As String ' break the number into parts
Dim suffix(10) As String 'trillion, billion .million etc
Dim Wordparts(10) As String 'add the number parts and suffix
Dim output As String = Nothing
Dim T, B, M, TH, H, C As String
formatnumber = Format(Input, "0000000000000.00") 'format the input number to a 16 characters string by suffixing and prefixing 0s
'
numparts(0) = primWord(Mid(formatnumber, 1, 1)) 'Trillion
numparts(1) = primWord(Mid(formatnumber, 2, 1)) 'hundred billion..x
numparts(2) = primWord(Mid(formatnumber, 3, 2)) 'billion
numparts(3) = primWord(Mid(formatnumber, 5, 1)) 'hundred million...x
numparts(4) = primWord(Mid(formatnumber, 6, 2)) 'million
numparts(5) = primWord(Mid(formatnumber, 8, 1)) 'hundred thousand....x
numparts(6) = primWord(Mid(formatnumber, 9, 2)) 'thousand
numparts(7) = primWord(Mid(formatnumber, 11, 1)) 'hundred
numparts(8) = primWord(Mid(formatnumber, 12, 2)) 'Tens
numparts(9) = primWord(Mid(formatnumber, 15, 2)) 'cents
suffix(0) = " Trillion "
suffix(1) = " Hundred " '....x
suffix(2) = " Billion "
suffix(3) = " Hundred " ' ....x
suffix(4) = " Million "
suffix(5) = " Hundred " ' .....x
suffix(6) = " Thousand "
suffix(7) = " Hundred "
suffix(8) = " "
suffix(9) = ""
For i = 0 To 9
If numparts(i) <> "" Then
Wordparts(i) = numparts(i) & suffix(i)
End If
T = Wordparts(0)
If Wordparts(1) <> "" And Wordparts(2) = "" Then
B = Wordparts(1) & " Billion "
Else
B = Wordparts(1) & Wordparts(2)
End If
If Wordparts(3) <> "" And Wordparts(4) = "" Then
M = Wordparts(3) & " Million "
Else
M = Wordparts(3) & Wordparts(4)
End If
If Wordparts(5) <> "" And Wordparts(6) = "" Then
TH = Wordparts(5) & " Thousand "
Else
TH = Wordparts(5) & Wordparts(6)
End If
H = Wordparts(7) & Wordparts(8)
If Wordparts(9) = "" Then
C = " and Zero Cents "
Else
C = " and " & Wordparts(9) & " Cents "
End If
Next
output = T & B & M & TH & H & C
Return output
End Function
Public Shared Function primWord(ByVal Num As Integer) As String
'This two dimensional array store the primary word convertion of numbers 0 to 99
primWord = ""
Dim wordList(,) As Object = {{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 "}, {21, "Twenty One "}, {22, "Twenty Two"}, {23, "Twenty Three"}, {24, "Twenty Four"}, {25, "Twenty Five"},
{26, "Twenty Six"}, {27, "Twenty Seven"}, {28, "Twenty Eight"}, {29, "Twenty Nine"}, {30, "Thirty "}, {31, "Thirty One "},
{32, "Thirty Two"}, {33, "Thirty Three"}, {34, "Thirty Four"}, {35, "Thirty Five"}, {36, "Thirty Six"}, {37, "Thirty Seven"},
{38, "Thirty Eight"}, {39, "Thirty Nine"}, {40, "Forty "}, {41, "Forty One "}, {42, "Forty Two"}, {43, "Forty Three"},
{44, "Forty Four"}, {45, "Forty Five"}, {46, "Forty Six"}, {47, "Forty Seven"}, {48, "Forty Eight"}, {49, "Forty Nine"},
{50, "Fifty "}, {51, "Fifty One "}, {52, "Fifty Two"}, {53, "Fifty Three"}, {54, "Fifty Four"}, {55, "Fifty Five"},
{56, "Fifty Six"}, {57, "Fifty Seven"}, {58, "Fifty Eight"}, {59, "Fifty Nine"}, {60, "Sixty "}, {61, "Sixty One "},
{62, "Sixty Two"}, {63, "Sixty Three"}, {64, "Sixty Four"}, {65, "Sixty Five"}, {66, "Sixty Six"}, {67, "Sixty Seven"}, {68, "Sixty Eight"},
{69, "Sixty Nine"}, {70, "Seventy "}, {71, "Seventy One "}, {72, "Seventy Two"}, {73, "Seventy Three"}, {74, "Seventy Four"},
{75, "Seventy Five"}, {76, "Seventy Six"}, {77, "Seventy Seven"}, {78, "Seventy Eight"}, {79, "Seventy Nine"},
{80, "Eighty "}, {81, "Eighty One "}, {82, "Eighty Two"}, {83, "Eighty Three"}, {84, "Eighty Four"}, {85, "Eighty Five"},
{86, "Eighty Six"}, {87, "Eighty Seven"}, {88, "Eighty Eight"}, {89, "Eighty Nine"}, {90, "Ninety "}, {91, "Ninety One "},
{92, "Ninety Two"}, {93, "Ninety Three"}, {94, "Ninety Four"}, {95, "Ninety Five"}, {96, "Ninety Six"}, {97, "Ninety Seven"},
{98, "Ninety Eight"}, {99, "Ninety Nine"}}
Dim i As Integer
For i = 0 To UBound(wordList)
If Num = wordList(i, 0) Then
primWord = wordList(i, 1)
Exit For
End If
Next
Return primWord
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBoxWord.Text = ConverttoWord.ConvertNum(TextBoxfigure.Text)
End Sub
End Class
enter code here