Numeric functions for ALB forwarding rule scripts support arithmetic operations and numeric comparisons.
add | sub | mul | div | mod | gt | ge | lt | le | floor | ceil
add
Calculates the sum of two numbers.
Syntax: add(n1, n2)
Parameters:
n1: the addend.n2: the other addend.
Return value: The sum of n1 + n2.
Example:
n1 = add(10, 20)
say(concat('n1=', n1))Output:
n1=30sub
Calculates the difference between two numbers.
Syntax: sub(n1, n2)
Parameters:
n1: the minuend (the number to subtract from).n2: the subtrahend (the number to subtract).
Return value: The difference of n1 - n2.
Example:
n1 = sub(10, 20)
say(concat('n1=', n1))Output:
n1=-10mul
Calculates the product of two numbers.
Syntax: mul(n1, n2)
Parameters:
n1: the multiplicand.n2: the other multiplicand.
Return value: The product of n1 × n2.
Example:
n1 = mul(10, 20)
say(concat('n1=', n1))Output:
n1=200div
Calculates the quotient of two numbers.
Syntax: div(n1, n2)
Parameters:
n1: the dividend.n2: the divisor.
Return value: The quotient of n1 / n2.
Example:
n1 = div(10, 20)
say(concat('n1=', n1))Output:
n1=0.5mod
Calculates the remainder after dividing one number by another.
Syntax: mod(n1, n2)
Parameters:
n1: the dividend.n2: the divisor.
Return value: The remainder of n1 % n2.
Example:
n1 = mod(35, 20)
say(concat('n1=', n1))Output:
n1=15Comparison functions
gt, ge, lt, and le compare two numeric values and return a boolean. All four functions take the same parameters.
Parameters:
n1: the first number to compare.n2: the second number to compare.
| Function | Syntax | Condition | Returns true when |
|---|---|---|---|
gt | gt(n1, n2) | greater than | n1 > n2 |
ge | ge(n1, n2) | greater than or equal to | n1 >= n2 |
lt | lt(n1, n2) | less than | n1 < n2 |
le | le(n1, n2) | less than or equal to | n1 <= n2 |
Returns false otherwise.
Query string variables such as$arg_numare strings. Usetonumber()to convert them to numbers before passing to a comparison function.
Examples:
Check whether num is greater than 10:
if and($arg_num, gt(tonumber($arg_num), 10)) {
say('num > 10')
}Request: /path1/path2/file?num=11. Output:
num > 10Check whether num is greater than or equal to 10:
if and($arg_num, ge(tonumber($arg_num), 10)) {
say('num >= 10')
}Request: /path1/path2/file?num=10. Output:
num >= 10Check whether num is less than 10:
if and($arg_num, lt(tonumber($arg_num), 10)) {
say('num < 10')
}Request: /path1/path2/file?num=9. Output:
num < 10Check whether num is less than or equal to 10:
if and($arg_num, le(tonumber($arg_num), 10)) {
say('num <= 10')
}Request: /path1/path2/file?num=10. Output:
num <= 10floor
Rounds a number down to the nearest integer.
Syntax: floor(n)
Parameter: n: the number to round down.
Return value: The largest integer less than or equal to n.
Example:
if $arg_num {
#The tonumber function converts a string to a number.
say(concat('floor: ', floor(tonumber($arg_num))))
}Request: /path1/path2/file?num=9.3. Output:
floor: 9ceil
Rounds a number up to the nearest integer.
Syntax: ceil(n)
Parameter: n: the number to round up.
Return value: The smallest integer greater than or equal to n.
Example:
if $arg_num {
#The tonumber function converts a string to a number.
say(concat('ceil: ', ceil(tonumber($arg_num))))
}Request: /path1/path2/file?num=9.3. Output:
ceil: 10