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!

HELP! Converting Numeric Value to Text in ASP

Status
Not open for further replies.

Anthony1312002

Programmer
Mar 4, 2004
146
US
I hope someone can help me. I was directed to a script that converts a numeric amount to text. As you can see it's in VB. I want to convert the SQL variable called CashAmount to text. Is there anyone out there that could give me a hand in changing the script so that I can use it in ASP?

set RSData1 = Cnt.Execute("SELECT SendersName,AccountNumber,Sum(Cash) AS CashAmount1 " _
& "FROM tblCashCheckAndList " _
& "WHERE CashDate >= #" & FDate & "# and " _
& "CashDate <= #" & SDate & "# and " _
& "SendersName = '" & PayOne & "'" _
& "GROUP BY SendersName, AccountNumber")


Do Until RSData1.EOF
Dim CashC1
CashC1 = CashC1 + RSData1("CashAmount1")
AccountOne = RSData1("AccountNumber")
RSData1.MoveNext
Loop


Conversion script

Private Sub Command1_Click()

'first set up two arrays to convert numbers to words
Dim BigOnes(9) As String
Dim SmallOnes(19) As String

'and populate them
BigOnes(1) = "Ten"
BigOnes(2) = "Twenty"
BigOnes(3) = "Thirty"
BigOnes(4) = "Forty"
BigOnes(5) = "Fifty"
BigOnes(6) = "Sixty"
BigOnes(7) = "Seventy"
BigOnes(8) = "Eighty"
BigOnes(9) = "Ninety"

SmallOnes(1) = "One"
SmallOnes(2) = "Two"
SmallOnes(3) = "Three"
SmallOnes(4) = "Four"
SmallOnes(5) = "Five"
SmallOnes(6) = "Six"
SmallOnes(7) = "Seven"
SmallOnes(8) = "Eight"
SmallOnes(9) = "Nine"
SmallOnes(10) = "Ten"
SmallOnes(11) = "Eleven"
SmallOnes(12) = "Twelve"
SmallOnes(13) = "Thirteen"
SmallOnes(14) = "Fourteen"
SmallOnes(15) = "Fifteen"
SmallOnes(16) = "Sixteen"
SmallOnes(17) = "Seventeen"
SmallOnes(18) = "Eighteen"
SmallOnes(19) = "Nineteen"

'format the incoming number to guarantee six digits
'to the left of the decimal point and two to the right
'and then separate the dollars from the cents
Text1.Text = Format(Text1.Text, "000000.00")
Dollars = Left(Text1.Text, 3)
Cents = Right(Text1.Text, 2)

Words = ""

'check to make sure incoming number is not too large
If Dollars > 999999 Then
Text2.Text = "Dollar amount is too large"
Exit Sub
End If

'separate the dollars into chunks
If Dollars = 0 Then
Words = "Zero"
Else

'first do the thousands
Chunk = Left(Dollars, 3)
If Chunk > 0 Then
GoSub ParseChunk
Words = Words & " Thousand"
End If

'do the rest of the dollars
Chunk = Right(Dollars, 3)
If Chunk > 0 Then
GoSub ParseChunk
End If
End If

'concatenate the cents and display
If Cents = 0 Then Cents = "No"
Words = Words & " and " & Cents & "/100"
Text2.Text = Words
Exit Sub


ParseChunk:
digits = Mid(Chunk, 1, 1)
If digits > 0 Then
Words = Words & " " & SmallOnes(digits) & " Hundred"
End If
digits = Mid(Chunk, 2, 2)
If digits > 19 Then
leftdigit = Mid(Chunk, 2, 1)
rightdigit = Mid(Chunk, 3, 1)
Words = Words & " " & BigOnes(leftdigit)
If rightdigit > 0 Then
Words = Words & " " & SmallOnes(rightdigit)
End If
Else
If digits > 0 Then
Words = Words & " " & SmallOnes(digits)
End If
End If
Return

End Sub
 
I may be off base here, but couldn't this be done more simply in SQL? You could format it more to your specs, but the general idea would be something along these lines:
Code:
set RSData1 = Cnt.Execute("SELECT SendersName,AccountNumber,[COLOR=red]CAST(Sum(Cash) AS varchar(10))[/color] AS CashAmount1 " _
            & "FROM tblCashCheckAndList " _
            & "WHERE CashDate >= #" & FDate & "# and " _
            & "CashDate <= #" & SDate & "#  and " _
            & "SendersName = '" & PayOne & "'" _
            & "GROUP BY SendersName, AccountNumber")

------------------------------------------------------------------------------------------------------------------------
If you don't have a sense of humor, you probably don't have any sense at all.
- Anonymous
 
in VB, to convert it, use CSTR(value)

although stik's method would save a webserver step and put the load on the dataserver


[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
" I always think outside the 'box', because I'm never inside the 'loop' " - DreX 2005
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top