A real number is one that displays a decimal part made of
one or two sections separated by
a Decimal Separator or Decimal Symbol.
A single precision decimal number is a value that can range
from –3.402823e38 and –1.401298e-45 if the
number is negative, or 1.401298e-45 and 3.402823e38
if the number is positive. To declare a variable for such a number, use the Single data type. Here is an
example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Distance As Single
%>
</body>
</html>
Instead of the AS Single expression, you can use the !
symbol as the type character. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Distance!
%>
</body>
</html>
A Single variable is initialized with 0. After declaring a Single variable, you can
declare it with the necessary value. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Distance!
Distance! = 195.408
Response.Write("Distance: " & Distance)
%>
</body>
</html>
To indicate that the variable must be treated as a
single-precision decimal number, type f or F on the right side of the
number. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Distance!
Distance! = 195.408f
%>
</body>
</html>
To convert a string to a long integer, call CSng(). Enter the value or the expression in the parentheses of CSng().
If the conversion is successful, CSng() produces a Single value.
A double-precision decimal variable is one that can hold a
large number between 1.79769313486231e308 and –4.94065645841247e–324 if the number is negative or
between
1.79769313486231E308 and 4.94065645841247E–324 if
the number is positive. To declare a variable that can store a large decimal
number with precision, use the Double keyword.
Here is an example of declaring a Double variable:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim TempFactor As Double
%>
</body>
</html>
If you want, you can omit the AS Double
expression but use the #
symbol instead to declare a Double variable. This can be done as follows:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim TempFactor#
%>
</body>
</html>
A double-precision variable is initialized with a 0
value. After declaring a Double variable, you can initialize
it with the needed value. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim TempFactor#
TempFactor# = 482
Response.Write("Temperature: " & TempFactor & " Degrees")
%>
</body>
</html>
To indicate that the variable being used must be
treated with double precision, enter r or R on its right
side. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim TempFactor#
TempFactor# = 482r
Response.Write("Temperature: " & TempFactor & " Degrees")
%>
</body>
</html>
To convert a value to a double-precision number, call CDbl()
by entering the value or the expression in the parentheses of CDbl().
If CDbl() succeeds, it produces a Double value.
The decimal data type can be used to declare a variable that would
hold significantly large values that can be stored in a combination of 128
bits. You declare such a variable using the Decimal keyword. The values
stored in a decimal variable can range from ±1.0 × 10−28
to ±7.9 × 1028 with a precision of 28 to 29 digits.
Because of this high level of precision, the Decimal data type is
suitable for currency values.
Like those of the other
numeric types, by default, a variable declared as Decimal is
initialized with 0. After declaring a decimal
variable, you can initialize it with a natural number. To indicate to the
compiler to reserve space enough to store a decimal value, add a d or D to the
right side of the value. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim DistanceBetweenPlanets As Decimal
DistanceBetweenPlanets = 592759797549D
%>
</body>
</html>
To convert a value or an expression to Decimal,
you can call CDec().
To declare a variable that
would hold a character, use the Char data type. Here is an
example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Letter As Char
%>
</body>
</html>
When declaring a Char variable, if you don't
initialize it, it gets an empty character. Otherwise,
after declaring a Char variable, you can initialize it
with a single character included in double-quotes. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Letter As Char
Letter = "W"
%>
</body>
</html>
To indicate that the value of the variable must be
treated as Char, when initializing it, you can type c or C on the right
side of the double-quoted value. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Letter As Char
Letter = "W"c
%>
</body>
</html>
To convert a value to Char, you can call CChar().
A string is an empty text, a letter, a word or a group
of words considered "as is". To declare a string variable, you
can use the String data
type. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Sentence As String
%>
</body>
</html>
If you want, you can replace the AS String
expression with
the $ symbol when declaring a string
variable. This can be done as follows:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Sentence$
%>
</body>
</html>
After declaring a String variable, by default, it
gets an empty string. To initialize
it with a string of your choice, include the string between
double quotes. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Sentence$
Sentence$ = "He called me"
%>
</body>
</html>
If you want to include a double-quote character in the
string, you can double it. Here is an example:
<%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim Sentence$ Sentence$ = "Then she said, ""I don't love you no more."" and I left" %> <% Response.Write(Sentence) %> </body> </html>
This would produce:
To convert a value to a string, call CStr()
and enter the value or the expression in its parentheses.
If the value is an appropriate date or time, CStr() would produces a
string that represents that date or that time value.
To declare a variable that can hold either date
values, time values, or both, you can use the Date
data type. After the variable has been declared, you will configure it to
the appropriate value. Here are two examples:
<%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim DateOfBirth As Date Dim KickOffTime As Date %> </body> </html>
As mentioned above, the Date data type is used for
both date and time. There are rules you must
observe when dealing with date and time values. The rules are defined in
the Regional Settings Properties of the Control Panel of the computer on
which the application is run. The rules for the dates are defined in the
Date property page:
The rules for the time values are defined in the Time
property page:
By default, a Date variable is initialized with
January 1st, 0001 at midnight as the starting value. After declaring a Date variable, you can initialize it
with an appropriate date or time or date and time value. The value can be
included in double-quotes. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim DateOfBirth As Date
DateOfBirth = "08/14/1982"
%>
<%
Response.Write("Date of Birth: " & DateOfBirth)
%>
</body>
</html>
A Date value can also be included between two # signs.
Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim DateOfBirth As Date
Dim KickOffTime As Date
DateOfBirth = "08/14/1982"
KickOffTime = #6:45:00 PM#
%>
<%
Response.Write("Date of Birth: " & DateOfBirth)
Response.Write("<br>Kick off Time: " & KickOffTime)
%>
</body>
</html>
|
Variables and Data Types
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment