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!

Calculating Form Values using ASP

Status
Not open for further replies.

JordanR

Technical User
Oct 3, 2002
182
US
Hello,
As ASP newbie I have been running into a lot of problems trying to hand code my own scripts. I have been using dreamweaver ultradev/MX but now I am trying to ween myself off.

My current problem is trying to calculate the values of form field that have a number or dollar amount in them.
ex.
price = request.form("price")
quantity = request.form("quantity")
total = quantity * price

Though this is a very brief description it is the basics of getting this to work I don't understand. Can someone help me with this?

Thanks,
JordanR
 
you can try converting the form variants into math friendly values like such.

Code:
dim rf,total,price,qty
set rf = request.form
price = rf("price")
qty = rf("quantity")

'check for blanks
if not trim(price) = "" then price = int(price)
if not trim(qty) = "" then qty = int(qty)

total = (price * qty)
response.write(total)

Megalene
If you crap it will stink!
 
One thing ive picked over the years... "Option Explicit" is your friend. Dont ever let the app declare data for you, you keep control of the data.
 
The easiest and most accurate way of doing it is using CCur() to Convert to Currency, like this:
Code:
price = CCur(request.form("price"))
quantity = CCur(request.form("quantity"))
total = quantity * price
Alternately if you're not going to actually use the "price" and "quantity" variables for anything then you can just use:
Code:
total = CCur(request.form("price")) * CCur(request.form("quantity"))
 
ah yes ccur() how fast i forget, nice catch genimuse!

Megalene
If you crap it will stink!
 
Wow!
This has been a great help.
Thank you All,

I think I got it down to a science now. We will see.

I am going to get a little creative with this form and see what happens.

I will be back if run into any problems.

JordanR
 
Hey All,
I have been able to get form field values to add, multipy and all but for some reason the CCur isn't giving me total that looks like $23.50 all i am getting is 23.50
Is there something I am doing wrong here?

here's the code:
<%
dim total

total = CCur(request.form(&quot;item1&quot;)) + CCur(request.form(&quot;item2&quot;))

response.write CCur(total)
%>
 
try using on your response.write this -

response.write formatcurrency(ccur(total),2)

Megalene
If you crap it will stink!
 
Teh only thing the CCur function does is to force currency arithmetic. It will not give you the $ format. You need to add these types of formatting steps by concatination or use the FormatCurrency function (preferred)

____________________________________________________
[sub]The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811[/sub]
onpnt2.gif
 
CCur() is used to convert something that resembles currency into a currency data subtype. It works great for processing a string into currency, as in the earlier example. Thus:
Code:
    a = &quot;$23.40&quot;
    b = &quot;19.25 &quot;
    c = CCur(a) + CCur(b)
    Response.Write c
will result in 42.65.

If you want to format something to look like currency then you would instead (or in addition) use the FormatCurrency() function. In the US this code:
Code:
    a = &quot;$23.40&quot;
    b = &quot;19.25 &quot;
    c = CCur(a) + CCur(b)
    Response.Write FormatCurrency(c)
will result in $42.65.
 
Great! Got it.
Now how would a + b*c = d look
my guess is:
d = Ccur(a) + (CCur(b)*CCur(c))
response.write FormatCurrency(d)

this has been great learning. Thanks again to all responders
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top