Convert Numbers To Text

VB Snippet

Convert numbers to text

This routine will convert a string representation of a number to it's text equivalent.

Try it here, although this is written in vbscript, the routine is the same.
As written, it will convert up to 999999999999999 (Trillions).

e.g. 123 converts to "One Hundred and Twenty Three"

    

Click to see versions in    java     vbscript

This line determines the size
    divisor = Array("Trillion ","Billion ","Million ","Thousand ","")

You only have to add to the beginning "Quadrillion " to add another dimension, or remove "Trillion " to reduce a dimension. You could then also remove "Billion " if you did not want billions etc. Note: any addition to the array must be at the beginning, and the additional word must also include a trailing " " (space).

VB code

Start a new project and add to Form 1
1 x text box (20 characters wide)
1 x label (wide and deep)
1 x command button

Option Explicit
'---------
Private Sub Command1_Click()
Label1.Caption = convertToText(Text1.text)
End Sub
'--------
Private Function convertToText(ByVal numbers As String) As String
Dim text As String, bits(90) As String
Dim sAnd(1) As String, hundred(1) As String
Dim flag As Integer, groups() As Integer
Dim firstDigit As Integer, secondDigit As Integer, thirdDigit As Integer
Dim H As Integer, A As Integer, D As Integer, x As Integer
Dim minus As String

Dim divisor As Variant
divisor = Array("Trillion ", "Billion ", "Million ", "Thousand ", "")
ReDim groups(UBound(divisor))

sAnd(0) = "": sAnd(1) = "and "
hundred(0) = "": hundred(1) = "Hundred "

If Mid$(numbers, 1, 1) = "-" Then
    minus = "minus "
    numbers = Mid$(numbers, 2)
End If

Do Until Mid$(numbers, 1, 1) <> "0"
    numbers = Mid$(numbers, 2)
Loop

If numbers = "" Then
    convertToText = "zero, or no data"
    Exit Function
End If

Dim i As Integer
For i = 1 To Len(numbers)
    If Not Mid$(numbers, i, 1) Like "[0-9]" Then
        convertToText = "invalid data"
        Exit Function
    End If
Next

If Len(numbers) > (UBound(divisor) + 1) * 3 Then
    convertToText = "outside of number range"
    Exit Function
End If

bits(0) = "": bits(1) = "One "
bits(2) = "Two ": bits(3) = "Three "
bits(4) = "Four ": bits(5) = "Five "
bits(6) = "Six ": bits(7) = "Seven "
bits(8) = "Eight ": bits(9) = "Nine "
bits(10) = "Ten ": bits(11) = "Eleven "
bits(12) = "Twelve ": bits(13) = "Thirteen "
bits(14) = "Fourteen ": bits(15) = "Fifteen "
bits(16) = "Sixteen ": bits(17) = "Seventeen "
bits(18) = "Eighteen ": bits(19) = "Nineteen "
bits(20) = "Twenty ": bits(30) = "Thirty "
bits(40) = "Forty ": bits(50) = "Fifty "
bits(60) = "Sixty ": bits(70) = "Seventy "
bits(80) = "Eighty ": bits(90) = "Ninety "

x = UBound(divisor)
Do Until Len(numbers) = 0
    groups(x) = CInt(Right(numbers, 3))
    If Len(numbers) <= 3 Then
        numbers = ""
    Else: numbers = Mid$(numbers, 1, Len(numbers) - 3)
    End If
    x = x - 1
Loop

For i = x + 1 To UBound(divisor) 'convert in groups - 111,222,333,444,555
    firstDigit = groups(i) \ 100
    secondDigit = (groups(i) - (groups(i) \ 100) * 100) \ 10
    thirdDigit = groups(i) - (firstDigit * 100) - (secondDigit * 10)

    If secondDigit = 1 Then
        secondDigit = secondDigit + 9 + thirdDigit
        thirdDigit = 0
    Else: secondDigit = secondDigit * 10
    End If

    If i < UBound(divisor) And (firstDigit + secondDigit + thirdDigit) > 0 Then
        flag = 1 'setting flag for "and" in last group
    End If
    If firstDigit > 0 Then H = 1 Else H = 0
    If (firstDigit > 0 Or (i = UBound(divisor) And flag = 1)) And _
        (secondDigit > 0 Or thirdDigit > 0) Then A = 1 Else A = 0
    If (firstDigit + secondDigit + thirdDigit) > 0 Then D = i Else D = UBound(divisor)

    text = text & bits(firstDigit) & hundred(H) & sAnd(A) & _
            bits(secondDigit) & bits(thirdDigit) & divisor(D)
Next

convertToText = minus & text
End Function


back     top   main page    vb snippets page      java snippets page     vbscript snippets page   email    Page last modified