Purpose
Use JavaScript to evaluate complex arithmetic expressions encompassing GuiXT variables.

GuiXT scripts only support operations with two operands, which may result in a series of script lines for complex calculations.

Solution
Use the JavaScript function "compute" given below to evaluate arithmetic expressions which encompass numeric constants and GuiXT variables.

Examples

GuiXT
Set V[x1] "1124,78"
Set V[x2] "5,39"

CallJS compute "r=x1+x2"

// display test message
Message "&V[r]"



 

Set V[amount]  "1280,35"
Set V[taxrate] "19"

CallJS compute "tax = amount * taxrate / 100"

// display test message
Message "&V[tax]"

 

The "compute" function returns a comma as decimal separator. You may change this statically in the JavaScript library or use the function  "compute_set_decimal_separator" to change the decimal separator dynamically in your script, depending on the SAP user settings, for example.

The result, if not an integer value,  is returned with 2 decimal places. You may specify a different number of decimals in the CallJS statement:

GuiXT
// set decimal separator for "compute"
CallJS compute_set_decimal_separator "."

CallJS compute "r = 2 / 3"  decimals=5

// display test message
Message "&V[r]"

 

All complex arithmetic expressions are supported (parentheses, operator precedence). As operators you may use +, -, *, / and % (modulo).

GuiXT
Set V[amount1] "128,99"
Set V[amount2] "7,20"
Set V[amount3] "0,98"
Set V[taxrate] "19"

CallJS compute "r = (amount1+amount2+amount3) * taxrate / 100"

// display test message
Message "&V[r]

The "compute" function also allows us to use the alternative notation

GuiXT

CallJS r =  compute "(amount1+amount2+amount3) * taxrate / 100"

 

JavaScript

Components
InputAssistant + Controls