Decimal Variables Types
A real number is one that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel of computers of most regular users:
On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.
A decimal number is said to have single precision if it can include a decimal part but the precision of the number is not important. In VBasic, such a number has 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 that can hold small decimal numbers with no concern for precision, use the Single data type. Here is an example:
Module Module1 Sub Main() Dim Distance As Single End Sub End Module
Instead of the AS Single expression, you can use the ! symbol as the type character. Here is an example:
Module Module1 Sub Main() Dim Distance! End Sub End Module
The ! symbol can also be used in other scenarios in VBasic. Whenever you use it, make sure the word that comes to its right is not the name of a variable.
A Single variable is initialized with 0. After declaring a Single variable, you can declare it with the necessary value. Here is an example:
Module Module1 Sub Main() Dim Distance! Distance! = 195.408 MsgBox(Distance) End Sub End Module
In some cases, the compiler may allocate more memory than necessary for the variable. For example, the above value is more suitable for another type as we will see in the next section. Otherwise, to indicate that the variable must be treated as a decimal number with single-precision, type f or F on the right side of the number. Here is an example:
Module Module1 Sub Main() Dim Distance! Distance! = 195.408f End Sub End Module
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.
While the Single data type can allow large numbers, it offers less precision. For an even larger number, VBasic provides the Double data type. This is used for a variable that would hold numbers that range from 1.79769313486231e308 to –4.94065645841247e–324 if the number is negative or from 1.79769313486231E308 to 4.94065645841247E–324 if the number is positive.
To declare a variable that can store large decimal numbers with a good level of precision, use the Double keyword.
Here is an example of declaring a Double variable:
Module Module1 Sub Main() Dim TempFactor As Double End Sub End Module
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:
Module Module1 Sub Main() Dim TempFactor# End Sub End Module
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:
Module Module1 Sub Main() Dim TempFactor# TempFactor# = 482 MsgBox(TempFactor) End Sub End Module
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:
Module Module1 Sub Main() Dim TempFactor# TempFactor# = 482r MsgBox(TempFactor) End Sub End Module
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 convert a value or an expression to Long, you can call CLng().
|Previous||Copyright © 2004-2007 FunctionX, Inc.||Next|