Click to See Complete Forum and Search --> : Asp and math equations???


darkfilz303
11-17-2005, 02:11 AM
Is it possible to do math equations using asp and if so can you please give an example... :confused:

buntine
11-17-2005, 02:29 AM
Dim a: a = 1 + 2

Yep. a equals 3.

Regards.

lmf232s
11-17-2005, 02:23 PM
<%=(2-1) + ((3*3)/2)%> = 5.5
<%=FormatNumber((2-1) + ((3*3)/2),1)%> = 5.5
<%=FormatNumber((2-1) + ((3*3)/2),0)%> = 6

You can do any math equation.

There are also some built in functions taht you can use
Abs(number) = returns absolute value for a number
Exp(Number) =The Exp function raises e to the power of a number.
Sqr(Number) = The Sqr function returns the square root of a positive number.

Here is a web site that lists some more functions.
http://www.devguru.com/technologies/vbscript/13896.asp

darkfilz303
11-17-2005, 05:48 PM
Ahhh....OK now I have a problem, I got my script to work when I plug in numbers, but now I am trying to get it to work using variables?? :confused:

Every time I add the number 2+2+2 I get 222, When I want the answer 6.

Here's the Script:

<% OPTION EXPLICIT %>
<% Dim a, b, c, %>
<% a = Request.Form("a") %>
<% b = Request.Form("b") %>
<% c = Request.Form("c") %>

<% = a + b + c %>

<html>
<head>
<title>Testing...</title>
<body>

<form method="post" action="levels.asp">
<input type="text" name="a" size="2">
<input type="text" name="b" size="2">
<input type="text" name="c" size="2">
<input type="submit">
</form>
</body>
</html>

buntine
11-17-2005, 08:47 PM
Ok. The reason this is happening is because the Form collection converts all data to Strings (everything in HTTP is handled as a character array).

You have to explicitely convert the variables to integers.

<%
Option Explicit

Dim a, b, c
a = CInt(Request.Form("a"))
b = CInt(Request.Form("b"))
c = CInt(Request.Form("c"))

Response.Write a + b + c
%>
<html>
<head>
<title>Testing...</title>
<body>

<form method="post" action="levels.asp">
<input type="text" name="a" size="2">
<input type="text" name="b" size="2">
<input type="text" name="c" size="2">
<input type="submit">
</form>
</body>
</html>

The function I used above is CInt (Cast Integer), which converts (or "casts") a value to an Integer. Note that this is not fool-proof at the moment as it will crash if a user enters a non-numeric value.

Also note, you do not need to prefix and post fix every line with <% and %>, respectively. Just open and close the blocks as I have done above.

Regards.

darkfilz303
11-17-2005, 09:14 PM
Thanx so much, that was a lil obvious, and I didn't see it, lol, well thanx so much for your help!

buntine
11-17-2005, 10:12 PM
No worries. :)

darkfilz303
11-17-2005, 11:41 PM
Ok, ok, now I wouldn't be a good webdesigner if I didn't ask questions, so well, I stumbled upon another problem...they just keep coming...

I am trying to divide using a variable and a number... using the script below, but it only works with mulitplication, addition, and subtraction, can someone help me make it work with division? :confused:

Heres the script:
<%
Option Explicit

Dim a, b, c
a = CInt(Request.Form("a"))
b = CInt(Request.Form("b"))
c = CInt(Request.Form("c"))

Response.Write (a + b) / c
%>
<html>
<head>
<title>Testing...</title>
<body>

<form method="post" action="levels.asp">
<input type="text" name="a" size="2">
<input type="text" name="b" size="2">
<input type="text" name="c" size="2">
<input type="submit">
</form>
</body>
</html>

buntine
11-18-2005, 01:21 AM
This is happening because the code is being executed before you even get a chance to enter any values.

Make sure it only runs when necessary.

<%
Option Explicit

Dim a, b, c

If Request.Form("submit") <> "" Then
a = CInt(Request.Form("a"))
b = CInt(Request.Form("b"))
c = CInt(Request.Form("c"))

Response.Write Round(((a + b) / c), 2)
End If
%>
<html>
<head>
<title>Testing...</title>
<body>

<form method="post" action="levels.asp">
<input type="text" name="a" size="2">
<input type="text" name="b" size="2">
<input type="text" name="c" size="2">
<input type="submit" name="submit">
</form>
</body>
</html>

Regards.

darkfilz303
11-18-2005, 02:16 AM
Thanx so much... ;)