Using Arithmetic Expressions

The following example shows the creation of a single expression script that uses an arithmetic expression to derive a sales price as a percentage of the total sales.

To create a new script

  1. Click the New button on the Catalog of Available Fields dialog. The Add Script Field dialog is then displayed.

  2. Enter a name for your script in the Script Name text box.

In the following image, the single expression script has been named percent sales.

To select the first field in the expression

The field is then displayed on the Script Code sheet, as shown in the following image.

In this example, the retail sales price is selected (it is qualified by the RetailSaleItem subclass name).

The sales price is to be divided by the total sales value and multiplied by 100 to derive the percent of sales figure. You must first select the division operator.

To select the arithmetic operator

To select the divisor

In this example, total price is selected, as shown in the following image.

As the formula stands at this point, the result is the price divided by the total price for all sales to this client.

To turn this fraction into a percentage, you need to multiply it by 100.

To change the result to a percentage

  1. Select the multiply operator from the Arithmetic operators list in the Commands list box.

    Alternatively, you could simply type the symbol for multiplication; that is, the asterisk (*).

  2. Type the multiplier, which in this case is the value 100.

The formula is then complete, as shown in the following image.

As the result is a decimal value rather than a string, the return type must be changed to reflect this. Having completed the expression, select the Return Type (which in this example has been set to Decimal(12:2)), apply the expression, and you are then ready to apply your script field to your report. These procedures are described fully in the previous subsection, "Deriving String Fields".

Another example of applying a simple arithmetic expression to derive a report field is described in "Using the Script Sheet" under "Using the Catalog of Available Fields Dialog", in Chapter 4. In that example, a single expression is developed in which an exchange rate report parameter is applied to a report total value to produce that total in another currency.

The Commands list box, Fields list box, and the Methods list box of the Add Script Field or Update Script Field dialog are described in: