INT213 – Lab 5
Number and String Functions
This Lab will look at the Number and String functions available in VBScript. These functions provide a variety of
useful tools for manipulating and formatting data both coming into and going out of a script. Without these
functions, script would have limited usefulness. In addition, using these functions along with Conditional
statements and Loops will allow you to produce powerful scripts.
If you want to see the variety of Functions available in VBScript look at this link:
http://www.w3schools.com/vbscript/vbscript_ref_functions.asp
Number Functions
Many scripts must deal with the manipulation of numbers. Financial calculations are just one area scripts must
process numeric input and output a formatted result. Therefore we will look briefly at “number functions” that are
built in to VBScript. Your text will show you several others that will not be dealt with here.
INT( ) and Cint( )
These functions deal with Integers (whole numbers; numbers without decimals) and perform a similar function
but in a different way.
INT( )
basically chops off any decimals and leaves a whole number. It does not round the value.
Cint( ),
on the other
hand, does the same thing but rounds the number to the nearest whole number. Here is an example:
Save as: INT213lab5a.asp
<%
Option explicit
'dimension the variables
Dim num1, num2, total
'assign some values
num1 = 23.556789
num2 = 5.5
'do some calculations
Response.write "This calculation does not use any functions<br>"
Response.write "multiply num1 * num2 = " & num1 * num2 & "<br><br>"
‘now use the INT( ) function
Response.write "Using the INT( ) function :
INT(num1) * num2 = " & INT(num1) * num2
& "<br><br>"
Response.write "Using the INT( ) for the entire calculation:
INT(num1 * num2) = " &
INT(num1 * num2)
Response.write "<br><br>"
Response.write "Now let’s use the Cint( ) functions" & "<br><br>"
'using Cint() with the same numbers as above
Response.write "Using the CInt( ) function :
Cint(num1) * num2 = " & Cint (num1) * _ num2 & "<br><br>"
Response.write "Now Cint () for the entire calculation:
Cint (num1 * num2) = " &
Cint (num1 * _
num2)
Response.write
"<br><br>"
Response.Write "Now you can see what impact choosing the correct Function can have on a script"
%>
**** This short demonstration illustrates the importance of understanding how and when to
use number functions. Logical errors that would be very difficult to find and correct can result
from improper use us these functions
.****
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
The other uses of the
CINT( )
and
INT( )
functions is to convert String Numbers to their pure numerical type.
Let’s look at a script using a textbox
Save as: INT213lab5b.asp
<%
‘get the number from the textbox and add the number twice (ex: 4 + 4)
Response.write
Request(“txtboxnum”) + Request(“txtboxnum”)
%>
<body>
<p></p>
<form name = “form1” id = “form1” method =”post” action=””>
<br><br>
<input type=”text” name=”txtboxnum” />
<br>
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '10
 Tim

Click to edit the document details