Many times we come across the situation of converting a number spelled into words or a monetary value spelled into dollars & cents (or equivalent currency). I found the following code over internet and changed to fit my need (as we all do) and generalized in such a way that it can be used for either just convert a number to words or monetary value to words. You can add different country code if needed.
1: Public Overloads Shared Function ConvertNumberInWords(ByVal value As String) As String
2: Return ConvertNumberInWords(value, "")
3: End Function 4: 5: Public Overloads Shared Function ConvertNumberInWords(ByVal value As String, ByVal countryCode As String) As String
6: Dim majorCurrency As String = String.Empty 7: Dim minorCurrency As String = String.Empty 8: 9: Select Case countryCode.ToUpper() 10: Case "GBR"
11: majorCurrency = "Pound"
12: minorCurrency = "Pence"
13: Case "USA", "CAN"
14: majorCurrency = "Dollar"
15: minorCurrency = "Cent"
16: End Select 17: 18: value = value.Replace(",", "").Replace("$", "")
19: value = value.TrimStart(CChar("0"))
20: 21: Dim decimalCount As Int32 = 0 22: For x As Int32 = 0 To value.Length - 1
23: If value(x).ToString = "." Then
24: decimalCount += 1 25: If decimalCount > 1 Then Throw New ArgumentException("Only monetary values are accepted")
26: End If 27: 28: If Not (Char.IsDigit(value(x)) Or value(x).ToString = ".") And Not (x = 0 And value(x).ToString = "-") Then
29: Throw New ArgumentException("Only monetary values are accepted")
30: End If 31: Next 32: 33: Dim returnValue As String = ""
34: Dim parts() As String = value.Split(CChar("."))
35: 36: If parts.Length > 1 Then 37: parts(1) = parts(1).Substring(0, 2).ToCharArray 'Truncates -- doesn't round.
38: End If 39: 40: Dim IsNegative As Boolean = parts(0).Contains("-")
41: If parts(0).Replace("-", "").Length > 18 Then
42: Throw New ArgumentException("Maximum value is $999,999,999,999,999,999.99")
43: End If 44: 45: If IsNegative Then 46: parts(0) = parts(0).Replace("-", "")
47: returnValue &= "Minus "
48: End If 49: 50: If parts(0).Length > 15 Then 51: returnValue &= HundredsText(parts(0).PadLeft(18, CChar("0")).Substring(0, 3)) & "Quadrillion "
52: returnValue &= HundredsText(parts(0).PadLeft(18, CChar("0")).Substring(3, 3)) & "Trillion "
53: returnValue &= HundredsText(parts(0).PadLeft(18, CChar("0")).Substring(6, 3)) & "Billion "
54: returnValue &= HundredsText(parts(0).PadLeft(18, CChar("0")).Substring(9, 3)) & "Million "
55: returnValue &= HundredsText(parts(0).PadLeft(18, CChar("0")).Substring(12, 3)) & "Thousand "
56: ElseIf parts(0).Length > 12 Then 57: returnValue &= HundredsText(parts(0).PadLeft(15, CChar("0")).Substring(0, 3)) & "Trillion "
58: returnValue &= HundredsText(parts(0).PadLeft(15, CChar("0")).Substring(3, 3)) & "Billion "
59: returnValue &= HundredsText(parts(0).PadLeft(15, CChar("0")).Substring(6, 3)) & "Million "
60: returnValue &= HundredsText(parts(0).PadLeft(15, CChar("0")).Substring(9, 3)) & "Thousand "
61: ElseIf parts(0).Length > 9 Then 62: returnValue &= HundredsText(parts(0).PadLeft(12, CChar("0")).Substring(0, 3)) & "Billion "
63: returnValue &= HundredsText(parts(0).PadLeft(12, CChar("0")).Substring(3, 3)) & "Million "
64: returnValue &= HundredsText(parts(0).PadLeft(12, CChar("0")).Substring(6, 3)) & "Thousand "
65: ElseIf parts(0).Length > 6 Then 66: returnValue &= HundredsText(parts(0).PadLeft(9, CChar("0")).Substring(0, 3)) & "Million "
67: returnValue &= HundredsText(parts(0).PadLeft(9, CChar("0")).Substring(3, 3)) & "Thousand "
68: ElseIf parts(0).Length > 3 Then 69: returnValue &= HundredsText(parts(0).PadLeft(6, CChar("0")).Substring(0, 3)) & "Thousand "
70: End If 71: 72: Dim hundreds As String = parts(0).PadLeft(3, CChar("0"))
73: hundreds = hundreds.Substring(hundreds.Length - 3, 3) 74: 75: If CInt(hundreds) <> 0 Then 76: If CInt(hundreds) < 100 AndAlso parts.Length > 1 Then returnValue &= " "
77: returnValue &= HundredsText(hundreds) & majorCurrency 78: If CInt(hundreds) <> 1 Then returnValue &= IIf(majorCurrency.Length > 0, majorCurrency & "s", "").ToString()
79: If parts.Length > 1 AndAlso CInt(parts(1)) <> 0 Then returnValue &= " and "
80: Else 81: returnValue &= IIf(majorCurrency.Length > 0, " No " & majorCurrency & "s", "").ToString()
82: If parts.Length > 1 AndAlso CInt(parts(1)) <> 0 Then returnValue &= " and "
83: End If 84: 85: If parts.Length = 2 Then 86: If CInt(parts(1)) <> 0 Then 87: returnValue &= HundredsText(parts(1).PadLeft(3, CChar("0")))
88: returnValue &= minorCurrency 89: If CInt(parts(1)) <> 1 Then returnValue &= IIf(minorCurrency.Length > 0, "s", "").ToString()
90: End If 91: End If 92: 93: Return returnValue 94: 95: End Function 96: 97: Private Shared Function HundredsText(ByVal value As String) As String
98: Dim Tens As String() = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}
99: Dim Ones As String() = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}
100: 101: Dim returnValue As String = ""
102: Dim IsSingleDigit As Boolean = True 103: 104: If CInt(value(0).ToString) <> 0 Then
105: returnValue &= Ones(CInt(value(0).ToString) - 1) & " Hundred "
106: IsSingleDigit = False 107: End If 108: 109: If CInt(value(1).ToString) > 1 Then
110: returnValue &= Tens(CInt(value(1).ToString) - 1) & " "
111: If CInt(value(2).ToString) <> 0 Then
112: returnValue &= Ones(CInt(value(2).ToString) - 1) & " "
113: End If 114: ElseIf CInt(value(1).ToString) = 1 Then
115: returnValue &= Ones(CInt(value(1).ToString & value(2).ToString) - 1) & " "
116: Else 117: If CInt(value(2).ToString) <> 0 Then
118: If Not IsSingleDigit Then 119: returnValue &= "and "
120: End If 121: returnValue &= Ones(CInt(value(2).ToString) - 1) & " "
122: End If 123: End If 124: 125: Return returnValue 126: 127: End Function