Tutorial 6

advertisement
Tutorial 6
Numeric Variables
General Concept
Procedure Level Variables
Variables declared within a procedure (subroutine or function) are known within
that particular procedure. As with other variables declared in Visual BASIC the
variables will have a three letter datatype keyword that will precede the variable name. A
summary of types and keywords is provided on the opening page of the Concept Lesson.
Integer Type
The integer type is restricted to values less than 32767 and more than -32768. If
fractional values are assigned they are rounded off to the nearest integer. An unusual
method is used in cases like intA = 4.5 The resulting value stored is 4. But when the
statement intA = 5.5 is executed, intA stores 6. When a value is exactly halfway between
the rule is to round up only if the resulting value would be even, otherwise round down.
Long Type
Used when larger integer values are needed.
Single
Used when fractional values are required. sngB = 1/3 returns .3333333 and the
value is stored as a repeating decimal.
Currency
Accuracy to 4 decimal places, this type is used whenever numeric variables must
hold values that represent dollars and cents. The values stored are exact and they use
the 'normal' method of rounding off. curC = 1/3 returns .3333 Only four places are
stored.
Initialization
Visual Basic initializes all new variables to the value of zero. String variables are
initialized to the empty string, and object variables contain the initial value 'nothing'.
Assigning values to Numeric Variables
Do not use commas, dollar signs, percent symbols or any other character other
than an optional sign, digits and possibly a decimal point. lngA = 9.3 E 6 assigns the
value of 93,000,000 to the value of the long variable lngA.
Calculations
The usual symbols ^, +, -, * and / are used in calculations involving numeric
variables. Calculations involving a mixture of integers and decimal values are computed
using decimal values, then rounded off as necessary when the result is stored.
Integer division is provided using the \ operator. 6 \ 4 results in 1 The decimal
portion is dropped off (truncated)
Download