RSS

Convert number to words in English

20 Mar

The methods below are rewritten from How to convert a numeric value into English words in Excel to VB.NET equivalents. It takes string type arguments of numbers like “12,345.6789”, rounds it down to the second decimal place which becomes “12,345.67” and spells it as “Twelve Thousand Three Hundred Forty Five Dollars and Sixty Seven Cents”.

Private Function ConvertNumberToENG(ByVal amount As String) As String

    Dim dollars, cents, temp
    Dim decimalPlace, count
    Dim place(9) As String
    place(2) = " Thousand "
    place(3) = " Million "
    place(4) = " Billion "
    place(5) = " Trillion "

    ' String representation of amount.
    amount = amount.Trim()
    amount = amount.Replace(",", "")
    ' Position of decimal place 0 if none.
    decimalPlace = amount.IndexOf(".")
    ' Convert cents and set string amount to dollar amount.
    If decimalPlace > 0 Then
        cents = GetTens(amount.Substring(decimalPlace + 1).PadRight(2, "0").Substring(0, 2))
        amount = amount.Substring(0, decimalPlace).Trim()
    End If

    count = 1
    Do While amount <> ""
        temp = GetHundreds(amount.Substring(Math.Max(amount.Length, 3) - 3))
        If temp <> "" Then dollars = temp & place(count) & dollars
        If amount.Length > 3 Then
            amount = amount.Substring(0, amount.Length - 3)
        Else
            amount = ""
        End If
        count = count + 1
    Loop

    Select Case dollars
        Case ""
            dollars = "No Dollars"
        Case "One"
            dollars = "One Dollar"
        Case Else
            dollars = dollars & " Dollars"
    End Select

    Select Case cents
        Case ""
            cents = " and No Cents"
        Case "One"
            cents = " and One Cent"
        Case Else
            cents = " and " & cents & " Cents"
    End Select

    ConvertNumberToENG = dollars & cents
End Function

' Converts a number from 100-999 into text
Function GetHundreds(ByVal amount As String) As String
    Dim Result As String
    If Not Integer.Parse(amount) = 0 Then
        amount = amount.PadLeft(3, "0")
        ' Convert the hundreds place.
        If amount.Substring(0, 1) <> "0" Then
            Result = GetDigit(amount.Substring(0, 1)) & " Hundred "
        End If
        ' Convert the tens and ones place.
        If amount.Substring(1, 1) <> "0" Then
            Result = Result & GetTens(amount.Substring(1))
        Else
            Result = Result & GetDigit(amount.Substring(2))
        End If
        GetHundreds = Result
    End If
End Function

' Converts a number from 10 to 99 into text.
Private Function GetTens(ByRef TensText As String) As String
    Dim Result As String
    Result = ""           ' Null out the temporary function value.
    If TensText.StartsWith("1") Then   ' If value between 10-19...
        Select Case Integer.Parse(TensText)
            Case 10 : Result = "Ten"
            Case 11 : Result = "Eleven"
            Case 12 : Result = "Twelve"
            Case 13 : Result = "Thirteen"
            Case 14 : Result = "Fourteen"
            Case 15 : Result = "Fifteen"
            Case 16 : Result = "Sixteen"
            Case 17 : Result = "Seventeen"
            Case 18 : Result = "Eighteen"
            Case 19 : Result = "Nineteen"
            Case Else
        End Select
    Else                                 ' If value between 20-99...
        Select Case Integer.Parse(TensText.Substring(0, 1))
            Case 2 : Result = "Twenty "
            Case 3 : Result = "Thirty "
            Case 4 : Result = "Forty "
            Case 5 : Result = "Fifty "
            Case 6 : Result = "Sixty "
            Case 7 : Result = "Seventy "
            Case 8 : Result = "Eighty "
            Case 9 : Result = "Ninety "
            Case Else
        End Select
        Result = Result & GetDigit(TensText.Substring(1, 1))  ' Retrieve ones place.
    End If
    GetTens = Result
End Function

' Converts a number from 1 to 9 into text.
Private Function GetDigit(ByRef Digit As String) As String
    Select Case Integer.Parse(Digit)
        Case 1 : GetDigit = "One"
        Case 2 : GetDigit = "Two"
        Case 3 : GetDigit = "Three"
        Case 4 : GetDigit = "Four"
        Case 5 : GetDigit = "Five"
        Case 6 : GetDigit = "Six"
        Case 7 : GetDigit = "Seven"
        Case 8 : GetDigit = "Eight"
        Case 9 : GetDigit = "Nine"
        Case Else : GetDigit = ""
    End Select
End Function
Advertisements
 
Leave a comment

Posted by on March 20, 2012 in .NET, VB

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: