Convert Numbers To Text

VBScript Snippet

Convert numbers to text

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

As written, it will convert up to 999999999999999 (Trillions).

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

    

Click to see versions in    vb     java

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).

VBScript code

 

Option Explicit
msgbox convertToText("123456789")

Private Function convertToText(numbers)
Dim minus,i,ii, checkNumbers
Dim text, bits(90)
Dim sAnd, hundred, divisor
Dim flag, groups()
Dim firstDigit, secondDigit, thirdDigit
Dim H, A, D, x

sAnd = array("","and ")
hundred = Array("","Hundred ")
divisor = Array("Trillion ","Billion ","Million ","Thousand ","")
redim groups(ubound(divisor))


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

checkNumbers = Array("0","1","2","3","4","5","6","7","8","9")
For i = 1 To Len(numbers)
  ii = 0
  do while ii < 10
    If Mid(numbers, i, 1) = checkNumbers(ii) Then exit do
      ii = ii+1
  loop
  if ii = 10 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