How To Convert Numbers To Text Using VB.NET




hello this a tutorial which helps you now what this actually do is , when you enter some numbers ie:1000 and click the button you will one thousand in the converted richtextbox so i am using a class here and i have 2 richtextbox and a button ok now lets make it add two rich textbox and a button now change button text to convert and add labels to make the program look good and now add a class and name it convert.vb add this codes inside the convert.vb



Public Class Convert

#Region " Constructor "

    Public Sub New(ByVal amount As Long, ByVal style As ConvertStyle)
        m_amount = amount
        m_style = style
    End Sub

#End Region


#Region " Public Methods"

    Public Function Convert() As String

        Dim convertedString As String = String.Empty

        Select Case Me.Style

            Case ConvertStyle.English
                convertedString = Me.EnglishStyle

        End Select

        Return convertedString
    End Function

#End Region


#Region " Private Methods"

#Region " English Style "


    Private Function EnglishStyle()
        If Amount = 0 Then Return "Zero"
        Dim amountString As String = Amount.ToString
        Dim result As String = String.Empty
        Dim tempResult As String = String.Empty
        Dim i As Integer = 3
        Dim newAmount As String = amountString
        Dim workedStringLength As Integer = 0
        Dim concatHigherDigitString As Boolean

        Do
            concatHigherDigitString = False
            If i > 3 Then concatHigherDigitString = True


            If newAmount.Length >= 4 Then
                newAmount = amountString.Substring(amountString.Length - i, 3)
            End If



            If concatHigherDigitString AndAlso CInt(newAmount) <> 0 Then
                result = ThreeDigitsConverter(CInt(newAmount)) & " " & HigherDigitEnglishStringArray(i / 3 + 1) & " " & result
            Else
                result = ThreeDigitsConverter(CInt(newAmount))
            End If


            workedStringLength += newAmount.Length
            newAmount = amountString.Substring(0, amountString.Length - workedStringLength)
            i += 3
        Loop Until amountString.Length <= workedStringLength

        Return RemoveSpaces(result)

    End Function


    Private Function ThreeDigitsConverter(ByVal amount As Integer) As String
        Dim amountString As String = amount.ToString


        Dim amountArray(amountString.Length - 1) As Integer
        For i As Integer = amountArray.Length To 1 Step -1
            amountArray(i - 1) = amountString.Substring(i - 1, 1)
        Next

        Dim j As Integer = 0
        Dim digit As Integer = 0
        Dim result As String = String.Empty
        Dim separator As String = String.Empty
        Dim higherDigitEnglishString As String = String.Empty
        Dim codeIndex As String = String.Empty


        For i As Integer = amountArray.Length To 1 Step -1
            j = amountArray.Length - i
            digit = amountArray(j)

            codeIndex = EnglishCodeArray(i - 1)
            higherDigitEnglishString = HigherDigitEnglishStringArray(CInt(codeIndex.Substring(0, 1)) - 1)


            If codeIndex = "1" Then
                result = result & separator & SingleDigitStringArray(digit)

            ElseIf codeIndex.Length = 2 And digit <> 0 Then

                If digit = 1 Then
                    Dim suffixDigit As Integer = amountArray(j + 1)
                    result = result & separator & TenthDigitStringArray(suffixDigit) & " " & higherDigitEnglishString
                    i -= 1

                Else
                    result = result & separator & DoubleDigitsStringArray(digit) & " " & higherDigitEnglishString
                End If

            ElseIf digit <> 0 Then
                result = result & separator & SingleDigitStringArray(digit) & " " & higherDigitEnglishString
            End If

            separator = " "
        Next

        Return result
    End Function

#End Region

    Private Function RemoveSpaces(ByVal word As String) As String
        Dim regEx As New System.Text.RegularExpressions.Regex("  ")
        Return regEx.Replace(word, " ").Trim
    End Function

#End Region


#Region " Property"

    Public ReadOnly Property Amount As Long
        Get
            Return m_amount
        End Get
    End Property

    Public ReadOnly Property Style As ConvertStyle
        Get
            Return m_style
        End Get
    End Property
#End Region


#Region " Fields"

    Private m_amount As Long
    Private m_style As ConvertStyle

    Private SingleDigitStringArray() As String = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"}
    Private DoubleDigitsStringArray() As String = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}
    Private TenthDigitStringArray() As String = {"Ten", "Eleven", "Tweleve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}

    Private HigherDigitEnglishStringArray() As String = {"", "", "Hundred", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion"}
    Private EnglishCodeArray() As String = {"1", "22", "3"}

#End Region


#Region " Enums"

    Public Enum ConvertStyle
        English
    End Enum

#End Region

End Class


make your form look alike this
and now double click the form and add this code


Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = String.Empty Then
            Me.RichTextBox1.Text = String.Empty
            Return
        End If
        Dim number As Long = CLng(Me.TextBox1.Text)
        Dim converter As New Convert(number, Convert.ConvertStyle.English)
        Me.RichTextBox1.Text = converter.Convert
    End Sub
End Class


working preview :



1 comments:

Post a Comment

Note: Only a member of this blog may post a comment.