Using Numeric Amper Variables in Functions


WebFOCUS stores all amper variables as strings in alphanumeric format whether they contain alphanumeric or numeric data or a mixture of the two. There are only two data types available to amper variables: alphanumeric and numeric.

Determining Amper Variable Data Type

Data typing for amper variables is determined by the data content only. As a result, using quotation marks around a numeric value in a -SET command has no effect on the data type of the amper variable.

For example, the following request stores numeric data in variables &A, &B, and &C:

-SET &A=12345;
-SET &B='12345';
-SET &C=123.45
-TYPE &A &B &C

The output shows that &A, &B, and &C all have the numeric data type:

12345 12345 123.45
N  N  N

Manipulating Amper Variables

When an amper variable is displayed, substituted, concatenated, or appended, there is no transformation of the value contained in the amper variable.


-SET &C=123.45



Also, consider the following:

-SET &D= &C;

The output shows that &D has the same value as &C and is also numeric:

123.45  N


The amper variable &F is created by concatenating &A and &C:

-SET &F =  &A | &C;

The output shows that the value of &F is the value of &A followed by the value of &C, and that the type is numeric:

12345123.45  N

The following example creates the amper variable &E by embedding an ampersand in the string. The ampersand is not recognized as the start of a variable name and is treated as an alphanumeric symbol in a string:

-SET &E = 1234&C;

The output shows that the variable is of type alphanumeric, not numeric. It is not the concatenation of the string '1234' with the variable &C:

1234&C  A

This same behavior can be produced with concatenation:

-SET &G = AT&|T;

The output is:


Using an Amper Variable in an Expression

When an amper variable is used in an expression, conversion may be required in order to process the expression. The amper variable used in the expression is generally seen as a literal, and its value is substituted in before the expression is processed. Under these circumstances, data conversion necessary to process the expression is performed. Numerics contained in amper variables are seen as integers. If the expression can be evaluated as integer, it will be.

In the following example, &C is set to 123.55. Then an expression creates &D by adding 100 to &C :

-SET &C=123.55;
-SET &D=&C + 100;

The output shows that &D is numeric and its value is 123.55+100 truncated to the integer 223 because integer arithmetic is used:

223  N

The following expression requires conversion to double precision, as the numeric literal (100.49) in the expression is not an integer:

-SET &C=123.55;
-SET &D=&C + 100.49;

The output shows that while the arithmetic was done by converting the value of &C to double precision, the result is truncated before being returned to &D:

224  N

If you want the result to retain the decimal places, you can set the DMPRECISION parameter to the number of decimal places you want returned to the resulting amper variable.

For example:

-SET &C=123.55;
-SET &D=&C + 100.49;

Now the result retains the decimal places:

224.04 N

Using Amper Variables as Function Parameters


How you treat numeric amper variables when passing them to a function depends on the data type of the function parameter.

Using a Numeric Amper Variable as a Numeric Function Parameter

When using a numeric amper variable as a numeric parameter in a function call, the amper variable is treated as a field. Since as a field, it has no specified type in either the Master File or the FOCEXEC, it takes the default data type of double precision.

Note that when the result is returned to an output variable, its type is determined by its content. If it has only numbers and a decimal point, it is numeric. If it contains other symbols, it is alphanumeric.

For example, the FTOA function converts a double or single precision number (D or F) to an alphanumeric string with the format specified within the parentheses of the second parameter:

FTOA (number_to_convert, '(format)', 'alpha_output_format' )

The following example sets &C to 123.55 and passes it to the FTOA function to be converted to an alphanumeric string with a dollar sign:

-SET &C=123.55;
-SET &G=FTOA(&C,'(D7.2M)','A11');  

The output shows that the string $123.55 has been returned to &G. Since it has a symbol other than numeric digits and a decimal point, its type is alphanumeric:

$123.55    A

In the following example, the format returned does not specify a dollar sign:

-SET &A=12345;                       
-SET  &G=FTOA(&A/100,'(D7.2)','A11');

Since the returned string contains only numeric digits and a decimal point, its type is numeric:

123.45    N

Note that if the number had another digit, it would be returned with a comma, and its type would be alphanumeric:

-SET &A=123456;                       
-SET  &G=FTOA(&A/100,'(D7.2)','A11');

The output is:

1,234.56    A

Using a Numeric Amper Variable as an Alphanumeric Function Parameter

When using a numeric amper variable as an alphanumeric parameter in a function call, you must convert the numeric value to an alphanumeric string before using it in order to avoid failure due to a format error. You can do this using one of the functions designed to convert numerics to alphanumeric, or you can concatenate an alphanumeric character to the numeric value in order to assign it an alphanumeric data type.

For example, the following converts &C to a string and returns the string to the variable &G. It then passes &G to the RJUST function, which right justifies the value and returns it to the variable &H:

-SET &C=123.55;                   
-SET  &G=FTOA(&C,'(D7.2M)','A11');
-SET &H = RJUST(11,&G,'A11');    
-TYPE &G &G.TYPE                  

The output is:

$123.55   A
  $123.55 A