Arithmetical calculations

RPL supports the following arithmetic functions:

Addition: +

Subtraction: -

Multiplication: *

Division: /

Modulus (remainder): %

Operator precedence is defined in Operator Precedence

Example:

${100 - x * x}
${x / 2}
${12 % 10}  

assuming that x is 5, the example produces this output:

75
2.5

Both operands must be expressions which evaluate to a numerical value. The example below will cause an error, since "5" is a string and not the number 5:

${3 * "5"} <#-- WRONG! -->  

There is an exception to the above rule. The + operator is used to concatenate strings as well as numbers. As a result, if a string is on one side of +  and a numerical value on the other side, the numerical value will be converted to a string (using the format appropriate for the language of the page) and then use the + as a string concatenation operator. For example: ${3 + "5"} will output 35.

Generally, RPL does not convert strings to numbers.

If you want only the integer part of the result of a calculation, you can use the int built-in. For example:

${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}  

Assuming that x is 5, the example produces this output:

2
1
1
-1
-1  

Comparison

This section describes how to compare two values, using examples of the if directive.

The usage of the if directive is: <#if expression>...</#if>, where expression must evaluate to a boolean value; otherwise, an error will terminate template processing. If the value of expression is true, the code between the begin and end tags will be processed; otherwise it will be skipped.

To test two values for equality, use the = operator (or == as in Java or C).

To test two values for inequality, use the != operator. For example, assume that user is “Big Joe”:

<#if user = "Big Joe">
  It is Big Joe
</#if>
<#if user != "Big Joe">
  It is not Big Joe
</#if>  

The user = "Big Joe" expression evaluates to the boolean true, so the output will be:

It is Big Joe

The expressions on both sides of the = or != must evaluate to a scalar. Furthermore, the two scalars must be of same type (i.e. strings can only be compared to strings and numbers can only be compared to numbers). Otherwise, an error will terminate template processing. Note that RPL does exact comparison, so string comparisons are case and white space sensitive. This means that "x", "x " and "X" are not equal values.

For numerical and date values, you can also use <, <=, >= and >. You cannot use these operators for strings.

Example:

<#if x <= 12>
  x is less or equivalent to 12
</#if>  

Keep in mind that RPL interprets > as the closing character of an RPL tag. For this reason, you must enclose the expression in parentheses, for example <#if (x > y)>. Alternately, you can use &gt; and &lt;, for example<#if x &gt; y>. Note that entity references such as &gt; &lt; are only supported in arithmetical comparisons, they are not supported in other RPL tags. As another alternative, you can use lt instead of <, lte instead of <=, gt instead of > and gte instead of >=.

Logical operations

RPL supports the following standard logical operators:

Logical or: ||

Logical and: &&

Logical not: !

These operators work only with boolean values. Otherwise, an error will terminate template processing.

Example:

<#if x < 12 && color = "green">
  We have less than 12 things, and they are green.
</#if>
<#if !hot> <#-- here hot must be a boolean -->
  It's not hot.
</#if>  

Next steps

Built-ins

Learn more

Operator Precedence