EdgeScript provides built-in numeric functions for arithmetic operations, comparisons, and rounding within CDN edge rules.
Arithmetic functions
add
Returns the sum of two numbers. For example, add(10, 20) returns 30.
Syntax
add(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The first addend. |
n2 | The second addend. |
Return value
Returns n1 + n2.
Example
n1 = add(10, 20)
say(concat('n1=', n1))Output: n1=30
sub
Returns the difference of two numbers. For example, sub(10, 20) returns -10.
Syntax
sub(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The minuend (the number to subtract from). |
n2 | The subtrahend (the number to subtract). |
Return value
Returns n1 - n2.
Example
n2 = sub(10, 20)
say(concat('n2=', n2))Output: n2=-10
mul
Returns the product of two numbers. For example, mul(10, 20) returns 200.
Syntax
mul(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The multiplicand. |
n2 | The multiplier. |
Return value
Returns n1 × n2.
Example
n3 = mul(10, 20)
say(concat('n3=', n3))Output: n3=200
div
Returns the quotient of two numbers. For example, div(10, 20) returns 0.5.
Syntax
div(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The dividend. |
n2 | The divisor. |
Return value
Returns n1 / n2.
Example
n4 = div(10, 20)
say(concat('n4=', n4))Output: n4=0.5
mod
Returns the remainder after dividing one number by another. For example, mod(35, 20) returns 15.
Syntax
mod(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The dividend. |
n2 | The divisor. |
Return value
Returns n1 % n2.
Example
n5 = mod(35, 20)
say(concat('n5=', n5))Output: n5=15
Comparison functions
All four comparison functions accept two numeric arguments and return true or false. Use tonumber() to convert string query parameters to numbers before comparing.
gt
Returns true if n1 is greater than n2; otherwise returns false. For example, gt(11, 10) returns true.
Syntax
gt(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The number to compare. |
n2 | The number to compare against. |
Return value
Returns true if n1 > n2; otherwise returns false.
Example
if and($arg_num, gt(tonumber($arg_num), 10)) {
say('num > 10')
}Request: /path1/path2/file?num=11
Response: num > 10
ge
Returns true if n1 is greater than or equal to n2; otherwise returns false. For example, ge(10, 10) returns true.
Syntax
ge(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The number to compare. |
n2 | The number to compare against. |
Return value
Returns true if n1 >= n2; otherwise returns false.
Example
if and($arg_num, ge(tonumber($arg_num), 10)) {
say('num >= 10')
}Request: /path1/path2/file?num=10
Response: num >= 10
lt
Returns true if n1 is less than n2; otherwise returns false. For example, lt(9, 10) returns true.
Syntax
lt(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The number to compare. |
n2 | The number to compare against. |
Return value
Returns true if n1 < n2; otherwise returns false.
Example
if and($arg_num, lt(tonumber($arg_num), 10)) {
say('num < 10')
}Request: /path1/path2/file?num=9
Response: num < 10
le
Returns true if n1 is less than or equal to n2; otherwise returns false. For example, le(10, 10) returns true.
Syntax
le(n1, n2)Parameters
| Parameter | Description |
|---|---|
n1 | The number to compare. |
n2 | The number to compare against. |
Return value
Returns true if n1 <= n2; otherwise returns false.
Example
if and($arg_num, le(tonumber($arg_num), 10)) {
say('num <= 10')
}Request: /path1/path2/file?num=10
Response: num <= 10
Comparison example
The following example reads the num query parameter from the request URL and evaluates it against the value 10 using all four comparison functions.
if and($arg_num, gt(tonumber($arg_num), 10)) {
say('num > 10')
}
if and($arg_num, ge(tonumber($arg_num), 10)) {
say('num >= 10')
}
if and($arg_num, lt(tonumber($arg_num), 10)) {
say('num < 10')
}
if and($arg_num, le(tonumber($arg_num), 10)) {
say('num <= 10')
}| Request | Response |
|---|---|
/path1/path2/file?num=10 | num <= 10 num >= 10 |
/path1/path2/file?num=11 | num > 10 num >= 10 |
/path1/path2/file?num=9 | num < 10 num <= 10 |
Rounding functions
floor
Rounds a number down to the nearest integer. For example, floor(9.3) returns 9.
Syntax
floor(n)Parameters
| Parameter | Description |
|---|---|
n | The number to round down. |
Return value
Returns the largest integer less than or equal to n.
Example
n = floor(tonumber($arg_num))
say(concat('floor: ', n))Request: /path1/path2/file?num=9.3
Response: floor: 9
ceil
Rounds a number up to the nearest integer. For example, ceil(9.3) returns 10.
Syntax
ceil(n)Parameters
| Parameter | Description |
|---|---|
n | The number to round up. |
Return value
Returns the smallest integer greater than or equal to n.
Example
n = ceil(tonumber($arg_num))
say(concat('ceil: ', n))Request: /path1/path2/file?num=9.3
Response: ceil: 10
Rounding example
The following example reads the num query parameter and outputs both the ceiling and floor values.
if $arg_num {
say(concat('ceil: ', ceil(tonumber($arg_num))))
say(concat('floor: ', floor(tonumber($arg_num))))
}Request: /path1/path2/file?num=9.3
Response: ceil: 10 floor: 9