Words To Numbers

This algorithm converts English words to decimal numbers.



									Public Shared Function WordsToNumbers(words As String) As ULong
	If String.IsNullOrEmpty(words) Then
		Return 0
	End If
	words = words.Trim()
	words += " "c

	Dim number As ULong = 0
	Dim singles As String() = New String() {"zero", "one", "two", "three", "four", "five", _
		"six", "seven", "eight", "nine"}
	Dim teens As String() = New String() {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", _
		"sixteen", "seventeen", "eighteen", "nineteen"}
	Dim tens As String() = New String() {"", "", "twenty", "thirty", "forty", "fifty", _
		"sixty", "seventy", "eighty", "ninty"}
	Dim powers As String() = New String() {"", "thousand", "million", "billion", "trillion", "quadrillion", _
		"quintillion"}

	For i As Integer = powers.Length - 1 To 0 Step -1
		If Not String.IsNullOrEmpty(powers(i)) Then
			Dim index As Integer = words.IndexOf(powers(i))

			If index >= 0 AndAlso words(index + powers(i).Length) = " "c Then
				Dim count As ULong = WordsToNumbers(words.Substring(0, index))
				number += count * CULng(Math.Pow(1000, i))
				words = words.Remove(0, index)
			End If
		End If
	Next

	If True Then
		Dim index As Integer = words.IndexOf("hundred")

		If index >= 0 AndAlso words(index + "hundred".Length) = " "c Then
			Dim count As ULong = WordsToNumbers(words.Substring(0, index))
			number += count * 100
			words = words.Remove(0, index)
		End If
	End If

	For i As Integer = tens.Length - 1 To 0 Step -1
		If Not String.IsNullOrEmpty(tens(i)) Then
			Dim index As Integer = words.IndexOf(tens(i))

			If index >= 0 AndAlso words(index + tens(i).Length) = " "c Then
				number += CUInt(i * 10)
				words = words.Remove(0, index)
			End If
		End If
	Next

	For i As Integer = teens.Length - 1 To 0 Step -1
		If Not String.IsNullOrEmpty(teens(i)) Then
			Dim index As Integer = words.IndexOf(teens(i))

			If index >= 0 AndAlso words(index + teens(i).Length) = " "c Then
				number += CUInt(i + 10)
				words = words.Remove(0, index)
			End If
		End If
	Next

	For i As Integer = singles.Length - 1 To 0 Step -1
		If Not String.IsNullOrEmpty(singles(i)) Then
			Dim index As Integer = words.IndexOf(singles(i) & " "c)

			If index >= 0 AndAlso words(index + singles(i).Length) = " "c Then
				number += CUInt(i)
				words = words.Remove(0, index)
			End If
		End If
	Next

	Return number
End Function
								


Example

									Dim data = "five quadrillion four hundred seventy eight trillion seven hundred seventy five billion five hundred forty four million eight hundred seventy nine thousand five hundred ninty nine"
Dim value = WordsToNumbers(data)
								


Output

									5478775544879599