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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if I have a number eg. 1004.25,

Status
Not open for further replies.

mbwmbk

Programmer
Dec 12, 2002
19
0
0
US
if I have a number eg. 1004.25,
I need to display 'one thousand four dollar and 25/100 cents' on the page.
How can I do it?
 
The easiest way I found to do this is to create a two dimensional array of the numbers from 1 to 19 and the corresponding words, then either in the same arary or another array do 20 to 90 and the word hundred for 100, then do mappings for thousand, million, etc

Then what you will want to do is something along the lines of:
If amount / 1000000 >= 1 then
'add the number of millions, use fix to round down
moneyStr = moneyStr & Fix(amount/1000000) & " million"

etc...


This can get fairly complicated fairly quickly without recursion, so since I happen to have written a function to do this before I'll post that.
Justa note, this does not handle decimals, but that functionality could be added in without to much work.

Code:
<%
Option Explicit

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

'function to convert numbers to words
Function cWord(num)
	Dim leftToProcess, wrkngWord, i

	If num = 0 Then
		cWord = &quot;&quot;
		Exit Function
	End If

	leftToProcess = cDbl(num)
	
	For i = 31 to 1 step -1
		If leftToProcess/cLng(words(i,0)) >= 1 Then
			If i >= 28 Then
				wrkngWord  = wrkngWord & cWord(Fix(leftToProcess/cLng(words(i,0)))) & words(i,1)
			Else
				wrkngWord = wrkngWord & words(i,1)
			End If
			leftToProcess = leftToProcess - (Fix(leftToProcess/cLng(words(i,0))) * cLng(words(i,0)))
		End If

		If leftToProcess < 1 And leftToProcess > 0 Then
			cWord = wrkngWord
			Exit For
		End If
	Next
	
	cWord = wrkngWord
End Function

If Request.Form(&quot;txtNumber&quot;) <> &quot;&quot; Then
	If isNumeric(Request.Form(&quot;txtNumber&quot;)) Then
		Response.Write Request.Form(&quot;txtNumber&quot;) & &quot; Converts To &quot; & cWord(Request.Form(&quot;txtNumber&quot;))
	Else
		Response.Write Request.Form(&quot;txtNumber&quot;) & &quot; is not a number&quot;
	End If
End If
%>
<form method=&quot;POST&quot; action=&quot;money.asp&quot;>
	Enter a Number: <input type=&quot;text&quot; name=&quot;txtNumber&quot;>
	<input type=&quot;submit&quot;>
</form>

To handle decimals you could treat the number as a string, split it on the decimal, then process the array entries indepndantly. This way You would get a set of words for the part before the decimal and a set for after the decimal.

Hope this helps,

-Tarwn

[sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Thanks a lot for your help. I've try to split the number on the decimal but I can't make it work. Can you give me more hints on how to get the decimal part working?

Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top