hasokeric
9/7/2016 - 9:17 AM

Convert dollar amounts to text (ex: for checks)

Convert dollar amounts to text (ex: for checks)

   Public Shared RptLiteralsDescription As String
    Public Shared RptLiteralsDiscAmt As String
    Public Shared RptLiteralsDocTranAmt As String
    Public Shared RptLiteralsInvoiceNum As String
    Public Shared RptLiteralsLDate As String
    Public Shared RptLiteralsLNetAmount As String
    Public Shared RptLiteralsLNo As String
    Public Shared RptLiteralsLOrderOf As String
    Public Shared RptLiteralsLPayTo As String

    Dim suffixes As String() = {"Thousand ", "Million ", "Billion ", "Trillion ", "Quadrillion ", "Quintillion ", "Sextillion "}
    Dim units As String() = {"", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "}
    Dim tens As String() = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety "}
    Dim digits As String() = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen"}

    Function ExpandPrice(ByVal Price As Decimal, Optional pSeparator As String = ".") As String
        Dim pPrice As String = Microsoft.VisualBasic.Strings.Format(Price, "##################.00")
        Console.WriteLine(pPrice)		
		
		Dim temp As New System.Text.StringBuilder()
				
        Dim parts As String() = pPrice.Split(pSeparator)
        Dim dollars As String = parts(0)
        Dim cents As String = parts(1)
        If CDbl(dollars) > 1 Then
            temp.Append(ExpandIntegerNumber(dollars) & "Dollars ")
            If CInt(cents) > 0 Then
                temp.Append("And ")
            End If
        ElseIf CDbl(dollars) = 0 Then
            temp.Append(ExpandIntegerNumber(dollars) & "Zero Dollars ")
            If CInt(cents) >= 0 Then
                temp.Append("And ")
            End If
        ElseIf CDbl(dollars) = 1 Then
            temp.Append(ExpandIntegerNumber(dollars) & "Dollar ")
        End If
        If CDbl(cents) > 1 Then
            temp.Append(ExpandIntegerNumber(cents) & "Cents")
        ElseIf CDbl(cents) = 0 Then
            temp.Append(ExpandIntegerNumber(cents) & "Zero Cents ")
        ElseIf CDbl(cents) = 1 Then
            temp.Append(ExpandIntegerNumber(cents) & "Cent ")
        End If
        Return temp.ToString()
    End Function

    Function ExpandIntegerNumber(pNumberStr As String) As String
		Dim temp2 As New System.Text.StringBuilder()
        Dim number As String = Microsoft.VisualBasic.StrDup(3 - Microsoft.VisualBasic.Len(pNumberStr) Mod 3, "0") & pNumberStr
        Dim i As Integer, j As Integer = -1
        Dim numPart As String
        For i = Microsoft.VisualBasic.Len(number) - 2 To 1 Step -3
            numPart = Microsoft.VisualBasic.Mid(number, i, 3)
            If CLng(numPart > 0) Then
                If j > -1 Then
                    temp2.Insert(0, suffixes(j), 1)
                End If
            End If
            temp2.Insert(0, GetNumberUnder1000Str(numPart), 1)
            j += 1
        Next
        Return temp2.ToString()
    End Function

    Function GetNumberUnder1000Str(pNumber As String) As String
        Dim temp1 As New System.Text.StringBuilder()
        If Microsoft.VisualBasic.Len(pNumber) = 3 Then
            If CLng(Microsoft.VisualBasic.Left(pNumber, 1)) > 0 Then
                temp1.Append(GetNumberUnder100Str(Microsoft.VisualBasic.Left(pNumber, 1)) & "Hundred ")
            End If
        End If
        temp1.Append(GetNumberUnder100Str(Microsoft.VisualBasic.Right("0" & pNumber, 2)))
        Return temp1.ToString()
    End Function


    Function GetNumberUnder100Str(pNumber As String) As String
        If pNumber > 19 Then
            Return tens(Microsoft.VisualBasic.Left(pNumber, 1) - 2) & units(Microsoft.VisualBasic.Right(pNumber, 1))
        ElseIf pNumber >= 10 And pNumber <= 19 Then
            Return digits(Microsoft.VisualBasic.Right(pNumber, 1))
        Else
            Return units(Microsoft.VisualBasic.Right(pNumber, 1))
        End If
    End Function

    Public Function SetLabels(p_RptLiteralsDescription As String, p_RptLiteralsDiscAmt As String, p_RptLiteralsDocTranAmt As String, p_RptLiteralsInvoiceNum As String, p_RptLiteralsLDate As String, p_RptLiteralsLNetAmount As String, p_RptLiteralsLNo As String, p_RptLiteralsLOrderOf As String, p_RptLiteralsLPayTo As String) As String
        RptLiteralsDescription = p_RptLiteralsDescription
        RptLiteralsDiscAmt = p_RptLiteralsDiscAmt
        RptLiteralsDocTranAmt = p_RptLiteralsDocTranAmt
        RptLiteralsInvoiceNum = p_RptLiteralsInvoiceNum
        RptLiteralsLDate = p_RptLiteralsLDate
        RptLiteralsLNetAmount = p_RptLiteralsLNetAmount
        RptLiteralsLNo = p_RptLiteralsLNo
        RptLiteralsLOrderOf = p_RptLiteralsLOrderOf
        RptLiteralsLPayTo = p_RptLiteralsLPayTo
        Return ""
    End Function