can anyone help me? i have a problem in converting a 1,000,000.00 number to words i always get result 1 MILLION THOUSAND PESOS ONLY which is wrong. it should be 1 MILLION PESOS ONLY

here is my code

Public Function ConvertMoneyToText(ByVal value As String) As String

        value = value.Replace(",", "").Replace("$", "")
        value = value.TrimStart("0")

        Dim decimalCount As Int32 = 0
        For x As Int32 = 0 To value.Length - 1
            If value(x).ToString = "." Then
                decimalCount += 1
                If decimalCount > 1 Then Throw New ArgumentException("Only monetary values are accepted")
            End If

            If Not (Char.IsDigit(value(x)) Or value(x).ToString = ".") And Not (x = 0 And value(x).ToString = "-") Then
                Throw New ArgumentException("Only monetary values are accepted")
            End If
        Next

        Dim returnValue As String = ""
        Dim parts() As String = value.Split(".")

        If parts.Length > 1 Then
            parts(1) = parts(1).Substring(0, 2).ToCharArray
        End If

        Dim IsNegative As Boolean = parts(0).Contains("-")
        If parts(0).Replace("-", "").Length > 18 Then
            Throw New ArgumentException("Maximum value is P999,999,999,999,999,999.99")
        End If

        If IsNegative Then
            parts(0) = parts(0).Replace("-", "")
            returnValue &= "Minus "
        End If

        If parts(0).Length > 15 Then
            returnValue &= HundredsText(parts(0).PadLeft(18, "0").Substring(0, 3)) & "Quadrillion "
            returnValue &= HundredsText(parts(0).PadLeft(18, "0").Substring(3, 3)) & "Trillion "
            returnValue &= HundredsText(parts(0).PadLeft(18, "0").Substring(6, 3)) & "Billion "
            returnValue &= HundredsText(parts(0).PadLeft(18, "0").Substring(9, 3)) & "Million "
            returnValue &= HundredsText(parts(0).PadLeft(18, "0").Substring(12, 3)) & "Thousand "
        ElseIf parts(0).Length > 12 Then
            returnValue &= HundredsText(parts(0).PadLeft(15, "0").Substring(0, 3)) & "Trillion "
            returnValue &= HundredsText(parts(0).PadLeft(15, "0").Substring(3, 3)) & "Billion "
            returnValue &= HundredsText(parts(0).PadLeft(15, "0").Substring(6, 3)) & "Million "
            returnValue &= HundredsText(parts(0).PadLeft(15, "0").Substring(9, 3)) & "Thousand "
        ElseIf parts(0).Length > 9 Then
            returnValue &= HundredsText(parts(0).PadLeft(12, "0").Substring(0, 3)) & "Billion "
            returnValue &= HundredsText(parts(0).PadLeft(12, "0").Substring(3, 3)) & "Million "
            returnValue &= HundredsText(parts(0).PadLeft(12, "0").Substring(6, 3)) & "Thousand "
        ElseIf parts(0).Length > 6 Then
            returnValue &= HundredsText(parts(0).PadLeft(9, "0").Substring(0, 3)) & "Million "
            returnValue &= HundredsText(parts(0).PadLeft(9, "0").Substring(3, 3)) & "Thousand "
        ElseIf parts(0).Length > 3 Then
            returnValue &= HundredsText(parts(0).PadLeft(6, "0").Substring(0, 3)) & "Thousand "
        End If

        Dim hundreds As String = parts(0).PadLeft(3, "0")
        hundreds = hundreds.Substring(hundreds.Length - 3, 3)
        If CInt(hundreds) <> 0 Then
            If CInt(hundreds) < 100 AndAlso parts.Length > 1 Then returnValue &= "And "
            returnValue &= HundredsText(hundreds) & "Peso"
            If CInt(hundreds) <> 1 Then returnValue &= "s"
            If parts.Length > 1 AndAlso CInt(parts(1)) <> 0 Then returnValue &= " And "
        Else
            If parts.Length = 2 Then
                returnValue &= "Pesos"
            Else
                returnValue &= "Pesos Only"
            End If
            If parts.Length > 1 AndAlso CInt(parts(1)) <> 0 Then returnValue &= " And "
        End If

        If parts.Length = 2 Then
            If CInt(parts(1)) <> 0 Then
                returnValue &= Microsoft.VisualBasic.Right(value, 2) & "/100" 'HundredsText(parts(1).PadLeft(3, "0"))
            End If
        End If

        Return returnValue

    End Function

    Private Function HundredsText(ByVal value As String) As String

        Dim Tens As String() = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}
        Dim Ones As String() = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}

        Dim returnValue As String = ""
        Dim IsSingleDigit As Boolean = True

        If CInt(value(0).ToString) <> 0 Then
            returnValue &= Ones(CInt(value(0).ToString) - 1) & " Hundred "
            IsSingleDigit = False
        End If

        If CInt(value(1).ToString) > 1 Then
            returnValue &= Tens(CInt(value(1).ToString) - 1) & " "
            If CInt(value(2).ToString) <> 0 Then
                returnValue &= Ones(CInt(value(2).ToString) - 1) & " "
            End If
        ElseIf CInt(value(1).ToString) = 1 Then
            returnValue &= Ones(CInt(value(1).ToString & value(2).ToString) - 1) & " "
        Else
            If CInt(value(2).ToString) <> 0 Then
                If Not IsSingleDigit Then
                    returnValue &= "and "
                End If
                returnValue &= Ones(CInt(value(2).ToString) - 1) & " "
            End If
        End If

        Return returnValue

    End Function

please help.

Recommended Answers

All 2 Replies

Hi, This is the code given by debasis das in this forum for some one.
See if this can help you..

Public Function NumToWord(ByVal numstr As Object) As String

        Dim newstr As String
        Dim tempstr As String = ""
        numstr = CDec(numstr)

        If numstr = 0 Then
            NumToWord = "Zero"
            Exit Function
        End If

        If numstr > 10 ^ 24 Then
            NumToWord = "Number too big"
            Exit Function
        End If

        If numstr >= 10 ^ 12 Then
            newstr = NumToWord(Int(numstr / 10 ^ 12))
            numstr = ((numstr / 10 ^ 12) - _
            Int(numstr / 10 ^ 12)) * 10 ^ 12
            If numstr = 0 Then
                tempstr = tempstr & newstr & "Bilion "
            Else
                tempstr = tempstr & newstr & "Bilion "
            End If
        End If




        If numstr >= 10 ^ 6 Then
            newstr = NumToWord(Int(numstr / 10 ^ 6))
            numstr = ((numstr / 10 ^ 6) - _
            Int(numstr / 10 ^ 6)) * 10 ^ 6
            If numstr = 0 Then
                tempstr = tempstr & newstr & "milion "
            Else
                tempstr = tempstr & newstr & "milion "
            End If
        End If

        If numstr >= 10 ^ 3 Then ' thousand
            newstr = NumToWord(Int(numstr / 10 ^ 3))
            numstr = ((numstr / 10 ^ 3) - Int(numstr / 10 ^ 3)) * 10 ^ 3
            If numstr = 0 Then
                tempstr = tempstr & newstr & "thousand "
            Else
                tempstr = tempstr & newstr & "thousand "
            End If
        End If

        If numstr >= 10 ^ 2 Then
            newstr = NumToWord(Int(numstr / 10 ^ 2))
            numstr = ((numstr / 10 ^ 2) - Int(numstr / 10 ^ 2)) * 10 ^ 2
            If numstr = 0 Then
                tempstr = tempstr & newstr & "hundred "
            Else
                tempstr = tempstr & newstr & "hundred "
            End If
        End If

        If numstr >= 20 Then
            Select Case Int(numstr / 10)
                Case 2
                    tempstr = tempstr & "twenty "
                Case 3
                    tempstr = tempstr & "thirty "
                Case 4
                    tempstr = tempstr & "forty "
                Case 5
                    tempstr = tempstr & "fifty "
                Case 6
                    tempstr = tempstr & "sixty "
                Case 7
                    tempstr = tempstr & "seventy "
                Case 8
                    tempstr = tempstr & "eighty "
                Case 9
                    tempstr = tempstr & "ninety "
            End Select
            numstr = ((numstr / 10) - Int(numstr / 10)) * 10
        End If

        If numstr > 0 Then
            Select Case numstr
                Case 1
                    tempstr = tempstr & "one "
                Case 2
                    tempstr = tempstr & "two "
                Case 3
                    tempstr = tempstr & "three "
                Case 4
                    tempstr = tempstr & "four "
                Case 5
                    tempstr = tempstr & "five "
                Case 6
                    tempstr = tempstr & "six "
                Case 7
                    tempstr = tempstr & "seven "
                Case 8
                    tempstr = tempstr & "eight "
                Case 9
                    tempstr = tempstr & "nine "
                Case 10
                    tempstr = tempstr & "ten "
                Case 11
                    tempstr = tempstr & "eleven "
                Case 12
                    tempstr = tempstr & "twelve "
                Case 13
                    tempstr = tempstr & "thirteen "
                Case 14
                    tempstr = tempstr & "fourteen "
                Case 15
                    tempstr = tempstr & "fifteen "
                Case 16
                    tempstr = tempstr & "sixteen "
                Case 17
                    tempstr = tempstr & "seventeen "
                Case 18
                    tempstr = tempstr & "eighteen "
                Case 19
                    tempstr = tempstr & "nineteen "
            End Select
            numstr = ((numstr / 10) - Int(numstr / 10)) * 10
        End If

        NumToWord = tempstr
    End Function
commented: thank you for sharing from my post. :) +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.