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.