Currency and Extended-currency

Currency variables are 8 byte binary representations of floating point numbers, which are considered to always have a fixed number of digits to the right of the decimal point. Currency numbers have a range of approximately -9.22*10^14 to +9.22*10^14, and Extended-currency have a range of -9.22*10^16 to +9.22*10^16.

The type-specifier character for Currency and Extended-currency floating-point is: @ and @@ respectively.

You can also use the DEFCUR or DEFCUX statement as described under Integers. They can also be declared using the CUR/CURRENCY or CUX/CURRENCYX keywords with the DIM statement. For example:

DIM I AS CUR

DIM J AS CURRENCYX

Currency variables (@) have up to 4 digits of precision after the decimal point, and are useful for prices and quantities where fractions of a cent are desired. Extended-currency variables (@@) have two digits of precision after the decimal point. They are optimized for financial calculations where fractions of a cent are not required.

The currency data types are especially useful for financial calculations, as they avoid the round-off errors associated with Single, Double, and Extended-precision numbers (which must be an exact power of two in order to be represented exactly). While many numbers can be represented exactly as a power of two, there are also many that cannot. For example, 1.10000002384185791 is the closest power of two to 1.1, in single precision.

So, when assigning numeric literal values to a Currency or Extended-currency variable, we recommend using a type specifier to ensure the value is given the intended precision. For example:

DIM x1 AS CUR

x1 = 1.0001@

DIM x2 AS CUX

x2 = 1.01@@

Internally, Currency and Extended-currency numbers are stored as Quad-integers with an implied decimal point (at 4 places for Currency, and at 2 places for Extended-currency). This approach ensures that all of the digits of the variables can be represented exactly.

Currency and Extended Currency perform a similar role as BCD variables in some BASIC dialects to ensure monetary values can be represented exactly; however, the internal storage of BCD variables and CUR/CUX differs substantially.

Delphi and Visual Basic both offer a currency data type that is identical to the PowerBASIC Currency variable.

See Also

Array Data Types

Bit Data Types

GUID Data Types

Object Data Types

Pointers

User Defined Types

Unions

Variant Data Types