Variable types CURRENCY, DECIMAL, VARIANT

A variable of type CURRENCY is stored as 8-byte integer, scaled by 10000 to give a fixed-point number with 15 digits to the left of the decimal point and 4 digits to the right. This provides a range of 922337203685477.5807 to -922337203685477.5808. Useful for calculations involving money, or for any fixed-point calculation where accuracy is particularly important.

 

A variable of type DECIMAL is stored as 12-byte integer, scaled by a power of 10.

 

A variable of type VARIANT can hold a value of type int, byte, word, double, long, BSTR, DATE, FLOAT, CURRENCY, DECIMAL, arrays, interface pointers and several other types. The first member (vt) of VARIANT holds type information. The next three words are used only by DECIMAL. Then follows 8-byte union that can hold value of other supported types. An union is a variable type whose members can have different types but they all share the same memory. Variables of type VARIANT are automatically cleared when go out of scope.

 

All these types support operator = (assign) and unary operators. To assign a string containing a number, val is not needed. Error if = cannot convert from another type.

Functions

All these types have the same set of arithmetic functions. These functions provide more precise results than calculations with operators. Common syntax:

 

var.function(parameters)

 

Here var is a variable of type CURRENCY, DECIMAL or VARIANT. Arguments can have any type, but before calculating they are converted to the type of var. Most functions return var itself.

 


 

var.add([left] right)

 

Adds left and right (var = left + right). If left is omitted, var is used (var = var + right).

 


 

var.sub([left] right)

 

Subtracts (var = left - right).

 


 

var.mul([left] right)

 

Multiplies (var = left * right).

 


 

var.div([left] right)

 

Divides (var = left / right).

 


 

var.round([number] [cDec])

 

Rounds number to cDec places after decimal point. Default for cDec is 0. Default for number is var.

 


 

var.fix([number])

 

Gets integer part of number. Default for number is var.

 


 

int var.cmp(numberorstring [flags])

 

Compares var and numberorstring. Returns -1 if var is less than numberorstring, 0 if equal, 1 if greater.

 

QM 2.3.5. Added flags. Flag 1 - case insensitive.

 

VARIANT functions

 

var.attach(a)

 

Here a is variable of type ARRAY, BSTR, VARIANT or interface pointer.

 

The function stores a into var without copying the associated data (as operator = does). Clears a.

 

Examples

VARIANT a
a.add(10 0.45)
 now a is 10.45
a.sub(a.mul(5 10) 0.45)
 now a is 49.55
a.round(2.52)
 now a is 3
a.round(2.52 1)
 now a is 2.5
a.fix(2.52)
 now a is 2
a = -a
 now a is -2
a = "string1"
a.add(" string2")
 now a is "string1 string2"