Function to convert numbers to words


Author
Message
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
I had the need for such a function and Juan Carlos posted a link to a thread with the function below, so I pasted in here for future reference:



Public Function ConvertMoneyToText(ByVal value As String) As String



'Takes a string value that translates as a decimal value of up to one pentillion (?) dollars.

'Note: Removing the comma for European currency will remove the decimal pointer.



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 'Truncates -- doesn't round.

End If



Dim IsNegative As Boolean = parts(0).Contains("-")

If parts(0).Replace("-", "").Length > 18 Then

Throw New ArgumentException("Maximum value is $999,999,999,999,999,999.99")

End If



If IsNegative Then

parts(0) = parts(0).Replace("-", "")

returnValue &= "Minus "

End If



'If you know the names of what is beyond quadrillion and feel the urge to do the

' rediculous just follow the pattern below.

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) & "Dollar"

If CInt(hundreds) <> 1 Then returnValue &= "s"

If parts.Length > 1 AndAlso CInt(parts(1)) <> 0 Then returnValue &= " and "

Else

returnValue &= " No Dollars"

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 &= HundredsText(parts(1).PadLeft(3, "0"))

returnValue &= "Cent"

If CInt(parts(1)) <> 1 Then returnValue &= "s"

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



Edhy Rijo

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Edhy Rijo - 15 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search