Numbers To Words

This algorithm converts decimal numbers to its English word representation.



									Public Shared Function NumbersToWords(num As ULong) As String
	Dim words As New StringBuilder()
	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", "ninety"}
	Dim powers As String() = New String() {"", "thousand", "million", "billion", "trillion", "quadrillion", _
		"quintillion"}

	If num >= 1000 Then
		For i As Integer = powers.Length - 1 To 0 Step -1
			Dim power As ULong = CULng(Math.Pow(1000, i))
			Dim count As ULong = (num - (num Mod power)) \ power

			If num > power Then
				words.Append(NumbersToWords(count) & " " & powers(i))
				num -= count * power
			End If
		Next
	End If

	If num >= 100 Then
		Dim count As ULong = (num - (num Mod 100)) \ 100
		words.Append(NumbersToWords(count) & " hundred")
		num -= count * 100
	End If

	If num >= 20 AndAlso num < 100 Then
		Dim count As ULong = (num - (num Mod 10)) \ 10
		words.Append(" " & tens(count))
		num -= count * 10
	End If

	If num >= 10 AndAlso num < 20 Then
		words.Append(" " & teens(num - 10))
		num = 0
	End If

	If num > 0 AndAlso num < 10 Then
		words.Append(" " & singles(num))
	End If

	Return words.ToString()
End Function
								


Example

									Dim data = 5478775544879599
Dim value = NumbersToWords(data).Trim()
								


Output

									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