Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a number into text

Status
Not open for further replies.

Anthony1312002

Programmer
Mar 4, 2004
146
US
Say I have a page that displays the number $456.28. Is there a script that will allow me to convert this to Four Hundred Fifty Six Dollars and Twenty Eight cents? Has anyone seen anything like that?
 
Additionally, this has been answered at least twice by myself. Whoops, correction, apparently three times since I just used the search above and found:
thread333-446945
thread333-878416
thread333-556894

They don't have anything for the decimal, but it should be fairly easy to split the number on "." ahead of time and process the first entry and concatenate on the second as x/100 dollars or as x cents.

-T

barcode_1.gif
 
Thanks for the great response Tarwin. I've got it working and it converts all dollar amounts to the corresponding english word. But how do I get it to recognize the cents For example. Getting $120.45 to convert to One Hundred Twenty dollars and forty five cents?

Here is what I have

Dim words(33,1)
words(1,0) = 1
words(1,1) = "One "
words(2,0) = 2
words(2,1) = "Two "
words(3,0) = 3
words(3,1) = "Three "
words(4,0) = 4
words(4,1) = "Four "
words(5,0) = 5
words(5,1) = "Five "
words(6,0) = 6
words(6,1) = "Six "
words(7,0) = 7
words(7,1) = "Seven "
words(8,0) = 8
words(8,1) = "Eight "
words(9,0) = 9
words(9,1) = "Nine "
words(10,0) = 10
words(10,1) = "Ten "
words(11,0) = 11
words(11,1) = "Eleven "
words(12,0) = 12
words(12,1) = "Twelve "
words(13,0) = 13
words(13,1) = "Thirteen "
words(14,0) = 14
words(14,1) = "Fourteen "
words(15,0) = 15
words(15,1) = "Fifteen "
words(16,0) = 16
words(16,1) = "Sixteen "
words(17,0) = 17
words(17,1) = "Seventeen "
words(18,0) = 18
words(18,1) = "Eighteen "
words(19,0) = 19
words(19,1) = "Nineteen "
words(20,0) = 20
words(20,1) = "Twenty "
words(21,0) = 30
words(21,1) = "Thirty "
words(22,0) = 40
words(22,1) = "Forty "
words(23,0) = 50
words(23,1) = "Fifty "
words(24,0) = 60
words(24,1) = "Sixty "
words(25,0) = 70
words(25,1) = "Seventy "
words(26,0) = 80
words(26,1) = "Eighty "
words(27,0) = 90
words(27,1) = "Ninety "
words(28,0) = 100
words(28,1) = "Hundred "
words(29,0) = 1000
words(29,1) = "Thousand "
words(30,0) = 1000000
words(30,1) = "Million "
words(31,0) = 1000000000
words(31,1) = "Billion "

'function to convert numbers to words
Function cWord(num)
Dim leftToProcess, wrkngWord, i
'if the number is 0, return an empty string (nothing left to add)
If num = 0 Then
cWord = ""
Exit Function
End If

'convert to number explicitly so later comparison will work on numbers, not strings
leftToProcess = cDbl(num)

'loop through all the numbers from array, starting with largest
For i = 31 to 1 step -1
'if the remainder we are still processing divided by the number we are checking against is greater than 1
If leftToProcess/cLng(words(i,0)) >= 1 Then
'if we are looking at 'Hundred' or higher, get the floor'd result of division and convert that to a word also (ie, 400 is cWord(4) & " hundred")
If i >= 28 Then
wrkngWord = wrkngWord & cWord(Fix(leftToProcess/cLng(words(i,0)))) & words(i,1)
Else
'otherwise just use the word
wrkngWord = wrkngWord & words(i,1)
End If

'subtract the portion we just converted to words
leftToProcess = leftToProcess - (Fix(leftToProcess/cLng(words(i,0))) * cLng(words(i,0)))
End If

'If we have less than one left then we are done (>0 handles negative numbers)
If leftToProcess < 1 And leftToProcess > 0 Then
cWord = wrkngWord
Exit For
End If
Next

'return the resulting string
cWord = wrkngWord
End Function


Writing it out to the page

If (CashC1) <> "" Then
If isNumeric((CashC1)) Then
Response.Write cWord((CashC1)) & "Dollars and"
Else
Response.Write (CashC1) & " VOID"
End If
End If
 
I would suggest doing an InStr test. If there is a dcimal in the value then split the value into an array so you have dollars in the first index and cents in the second. At that point it ought to be fairly simple to call cWord on both array indexes and append dollars to the first and cents to the second.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top