Page

4.3.3- Arithmetic Calculations

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 04. Variables.

Not tagged.
<< 4.3.2- Comparison OperatorsChapter44.3.4- Logical Operators >>

Arithmetic Calculations

The arithmetic operations available in VBScript are:

 

Addition

+

Exponentiation

^

Subtraction

-

Negation

-

Multiplication

*

Modulus

MOD

Division

/

Integer Division

\

 

Here is a very simple example: we'll assign values to the variables Number1 and Number2 before adding them together, and assigning the result to a third variable, Number3:

 

Number1 = 14

Number2 = 12

Number3 = Number1 + Number2

 

Because of this, Number3 will contain the value 26.

 

You can also use brackets (parentheses) to influence the order in which a calculation is performed. For example, in the following code we divide the variable Number2 by 6 and add the result to the variable Number1:

Quick reminder, normal mathematical procedure is start inside the innermost parentheses and from left to right performing exponentiation. Next go left to right performing multipication and division and then finally go left to right performing addition and subtraction. Then repeat the above steps again for next outer set of parenthesis, until you've calculated the expression.

 

Number1 = 14

Number2 = 18

Number3 = Number1 + (Number2/6)

 

First, the computer evaluates the contents of the brackets, following normal mathematical procedure: Number2 divided by 6 yields the result 3. This is added to the value of Number1, and the result of this – 17 – is assigned to the variable Number3.

 

The most common mistake is to assign values using quotes or hashes:

 

Number1 = "14"

Number2 = 18

Number3 = Number1 + Number2

 

Even if this didn't generate an error, it certainly wouldn't hold the value 32. Let's have a go at a quick example that performs a simple calculation.

Try It Out – Using Variables to Perform a Simple Calculation

OK, we've seen lots of theory, so now it's time to try out an example. We're going to perform a simple calculation of tax. To do this we need to declare three variables: one for the earnings, one for the tax percentage, and one for the total. We're going to deduct the earnings by whatever percentage the tax rate is set at and display the final income in an ASP page.

 

1.    Type the following program into your ASP editor:

 

<%Option Explicit%>

<HTML>

<HEAD>

<TITLE>Declaring Variables</TITLE>

</HEAD>

<BODY>

<%

  Dim intEarn, intTax, intTotal

  intEarn = 150

  intTax = 20

  intTotal = intEarn - ((intEarn/100)*intTax)

%>

 

<B><P>Your total earnings after tax are $<% = intTotal %> </P></B>

</BODY>

</HTML>

 

2.    Save it as taxcalc.asp in your inetpub\wwwroot\BegASPFiles folder.

3.    Now start your browser and run taxcalc.asp.

Chapter4_image003

Please note that all of the Try-It-Outs in this chapter are available for download on our web site at http://www.wrox.com/WileyCDA/WroxTitle/productCd-0764543636,descCd-download_code.html.

How It Works

There are only six lines of ASP code in this program. The first, of course, is our Option Explicit statement. The next declares three variables, intEarn for the earnings, intTax for the tax rate and intTotal for our final amount:

 

  Dim intEarn, intTax, intTotal

 

In the next line, we set the value of the earnings to 150, and the tax rate to 20:

 

  intEarn = 150

  intTax = 20

 

The intTotal variable is where the calculation is performed. To gain our percentage, we first have to calculate what 20% of the earnings are: this is done by dividing the earnings by 100 and multiplying the result by the tax rate. Brackets are used to indicate the order of calculation within this expression:

 

  intTotal = intEarn - ((intEarn/100)*intTax)

 

Finally, we return the value of intTotal, embedded in normal HTML code:

 

<B><P>Your total earnings after tax are $<% = intTotal %> </B></P>

 

You could calculate tax deductions for any percentage rate and any earnings, by altering the values of variables intEarn and intTax.

 

<< 4.3.2- Comparison OperatorsChapter44.3.4- Logical Operators >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |