Number Variables
OpenOffice.org Basic supports five basic types for processing numbers:
- Integer
- Long Integer
- Single
- Double
- Currency
Integer Variables
Integer variables can store any whole number between -32768 and 32767. An integer variable can take up to two bytes of memory. The type declaration symbol for an integer variable is %. Calculations that use integer variables are very fast and are particularly useful for loop counters. If you assign a floating point number to an integer variable, the number is rounded up or down to the next whole number.
Example declarations for integer variables:
Dim Variable As Integer
Dim Variable%
Long Integer Variables
Long integer variables can store any whole number between –2147483648 and 2147483647. A long integer variable can takes up to four bytes of memory. The type declaration symbol for a long integer is &. Calculations with long integer variables are very fast and are particularly useful for loop counters. If you assign a floating point number to a long integer variable, the number is rounded up or down to the next whole number.
Example declarations for long integer variables:
Dim Variable as Long
Dim Variable&
Single Variables
Single variables can store any positive or negative floating point number between 3.402823 x 1038 and 1.401298 x 10-45. A single variable can take up to four bytes of memory. The type declaration symbol for a single variable is !.
Originally, single variables were used to reduce the computing time required for the more precise double variables. However, these speed considerations no longer apply, reducing the need for single variables.
Example declarations for single variables:
Dim Variable as Single
Dim Variable!
Double Variables
Double variables can store any positive or negative floating point numbers between 1.79769313486232 x 10308 and 4.94065645841247 x 10-324. A double variable can take up to eight bytes of memory. Double variables are suitable for precise calculations. The type declaration symbol is #.
Example declarations of double variables:
Dim Variable As Double
Dim Variable#
Currency Variables
Currency variables differ from the other variable types by the way they handle values. The decimal point is fixed and is followed by four decimal places. The variable can contain up to 15 numbers before the decimal point. A currency variable can store any value between -922337203685477.5808 and +922337203685477.5807 and takes up to eight bytes of memory. The type declaration symbol for a currency variable is @.
Currency variables are mostly intended for business calculations that yield unforeseeable rounding errors due to the use of floating point numbers.
Example declarations of currency variables:
Dim Variable As Currency
Dim Variable@
The handling of Basic Currency type is not reliable. Issue 31001 Issue 54049 Issue 91121 Issue 107277 are still not corrected on OpenOffice.org version 3.1.1. |
Specification of Explicit Numbers
Numbers can be presented in several ways, for example, in decimal format or in scientific notation, or even with a different base than the decimal system. The following rules apply to numerical characters in OpenOffice.org Basic:
Whole Numbers
The simplest method is to work with whole numbers. They are listed in the source text without a comma separating the thousand figure:
Dim A As Integer
Dim B As Double
A = 1210
B = 2438
The numbers can be preceded by both a plus (+) or minus (-) sign (with or without a space in between):
Dim A As Integer
Dim B As Double
A = + 121
B = - 243
Decimal Numbers
When you type a decimal number, use a period (.) as the decimal point. This rule ensures that source texts can be transferred from one country to another without conversion.
Dim A As Integer
Dim B As Integer
Dim C As Double
A = 1223.53 ' is rounded
B = - 23446.46 ' is rounded
C = + 3532.76323
You can also use plus (+) or minus (-) signs as prefixes for decimal numbers (again with or without spaces).
If a decimal number is assigned to an integer variable, OpenOffice.org Basic rounds the figure up or down.
Exponential Writing Style
OpenOffice.org Basic allows numbers to be specified in the exponential writing style, for example, you can write 1.5e-10 for the number 1.5 x 10-10 (0.00000000015). The letter "e" can be lowercase or uppercase with or without a plus sign (+) as a prefix.
Here are a few correct and incorrect examples of numbers in exponential format:
Dim A As Double
A = 1.43E2 ' Correct
A = + 1.43E2 ' Correct (space between plus and basic number)
A = - 1.43E2 ' Correct (space between minus and basic number)
A = 1.43E-2 ' Correct (negative exponent)
A = 1.43E -2 ' Incorrect (spaces not permitted within the number)
A = 1,43E-2 ' Incorrect (commas not permitted as decimal points)
A = 1.43E2.2 ' Incorrect (exponent must be a whole number)
Note, that in the first and third incorrect examples that no error message is generated even though the variables return incorrect values. The expression
A = 1.43E -2
is interpreted as 1.43 minus 2, which corresponds to the value -0.57. However, the value 1.43 x 10-2 (corresponding to 0.0143) was the intended value. With the value
A = 1.43E2.2
OpenOffice.org Basic ignores the part of the exponent after the decimal point and interprets the expression as
A = 1.43E2
Hexadecimal Values
In the hexadecimal system (base 16 system), a 2-digit number corresponds to precisely one byte. This allows numbers to be handled in a manner which more closely reflects machine architecture. In the hexadecimal system, the numbers 0 to 9 and the letters A to F are used as numbers. An A stands for the decimal number 10, while the letter F represents the decimal number 15. OpenOffice.org Basic lets you use whole numbered hexadecimal values, so long as they are preceded by &H.
Dim A As Long
A = &HFF ' Hexadecimal value FF, corresponds to the decimal value 255
A = &H10 ' Hexadecimal value 10, corresponds to the decimal value 16
Octal Values
OpenOffice.org Basic also understands the octal system (base 8 system), which uses the numbers 0 to 7. You must use whole numbers that are preceded by &O.
Dim A As Long
A = &O77 ' Octal value 77, corresponds to the decimal value 63
A = &O10 ' Octal value 10, corresponds to the decimal value 8
fr:FR/Documentation/BASIC Guide/Numbers hu:HU/Documentation/BASIC Guide/Numbers it:IT/Documentation/BASIC Guide/Numbers ja:JA/Documentation/BASIC Guide/Numbers zh:ZH/Documentation/BASIC Guide/Numbers
Content on this page is licensed under the Public Documentation License (PDL). |