Skip to main content

Arithmetic Operators

Beeceptor's template engine implements fundamental arithmetic operations for dynamic response generation. The operations can be performed on numeric values from request parameters, body content, or static values. The template expressions use prefix notation, where operators precede their operands (e.g., {{add 1 2}} instead of 1 + 2).

math-operators-add-substract-demo

Most of the operations support multiple arguments and handle type coercion for numeric strings, with non-numeric values treated as zero or error emitted. Refer below the supported operators and their usage to build dynamic mock responses.

Note:

  1. By default, template engine is off. You need to explicitly mark a rule to enable usage of operators and If/Else constructs.
  2. If you have a use-case for an operator that isn't listed below, please contact our Support Team. They can assist you in prioritizing your request.

add

The add operator sums a list of numbers, which can be a combination of request parameters and static values.

Example:

The following template picks price and tax values from the request payload (JSON) and computes total in the HTTP response:

{
"total": {{add (body 'price') (body 'tax') 5}}
}

When the request body is {"price": 100, "tax": 10}, the response will be:

{
"total": 115
}

Additional Examples:

  • {{add 10 5 2}}: Adds a static list of numbers (10, 5, and 2).
  • {{add (body 'price') 2}}: Combines dynamic values (e.g., from the request body) with static values in a single operation.
  • {{add '10' 5.4 (body 'price')}}: Demonstrates how you can mix data types (strings, decimals, and dynamic values).
  • {{add 'abc' 2 (queryParam 'type')}}: In this example, any invalid values (non-numeric or NaN) will be ignored and treated as zero.

subtract

The subtract operator subtracts subsequent numbers from the first number. This is useful when you need to calculate differences between values, such as remaining balance, deductions, or similar operations.

Example:

{
"remaining": {{subtract (queryParam 'balance') (queryParam 'withdrawal')}}
}

If the request URL contains ?balance=1000&withdrawal=200, the response will be:

{
"remaining": 800
}

Additional Examples:

  • {{subtract 100 25 10}}: Subtracts 25 and 10 from 100, resulting in 65.
  • {{subtract (body 'total') 5}}: Subtracts a static value from a dynamic value retrieved from the request body.
  • {{subtract (queryParam 'initial') (queryParam 'deduction') 50}}: Subtracts two dynamic values, followed by a static value.
  • {{subtract '200' 50 (body 'discount')}}: Demonstrates mixed data types, where invalid values like strings ('200') will be treated as numbers if they can be converted, or ignored if not valid.

multiply

The multiply operator multiplies a list of numbers, which can be a combination of static values or dynamic data extracted from the request.

{
"total": {{multiply (body 'quantity') (body 'unitPrice')}}
}

If the request body contains {"quantity": 10, "unitPrice": 5}, the response will be:

{
"total": 50
}

Additional Examples:

  • {{multiply 5 4 3}}: Multiplies the static values 5, 4, and 3, resulting in 60.
  • {{multiply (body 'quantity') 2}}: Multiplies a dynamic value from the request body with a static value.
  • {{multiply (queryParam 'length') (queryParam 'width') (queryParam 'height')}}: Multiplies multiple dynamic values from query parameters to calculate volume, for example.
  • {{multiply '10' 2.5 (body 'factor')}}: This example uses mixed data types, where non-numeric or invalid values will be assumed as zero.

divide

The divide operator divides the first number by subsequent numbers. It's useful for calculating ratios, averages, or percentages.

Example:

{
"average": {{divide 100 2}}
}

This divides 100 by 2, resulting in the following response:

{
"average": 50
}

Note: Division by zero will result in an error, as dividing any number by zero is undefined.

modulo

The modulo operator returns the remainder after dividing the first number by the second number. This is useful for cyclic operations or determining if a number is even or odd.

Example:

{
"remainder": {{modulo 7 3}}
}

This returns the remainder of 7 divided by 3, resulting in:

{
"remainder": 1
}

Note: Computing modulo with zero will result in an error, as division by zero is undefined.

Additional Examples:

  • {{modulo 10 3}}: Returns 1 (remainder when 10 is divided by 3)
  • {{modulo (body 'total') 2}}: Checks if a number from request body is even (remainder 0) or odd (remainder 1)
  • {{modulo (queryParam 'items') 5}}: Useful for cyclic operations, like rotating through 5 different states
  • {{modulo '15' 4}}: Shows how string numbers are automatically converted, returns 3

floor

The floor operator rounds a number down to the nearest integer. This is useful when you need to remove decimal places and always round downward.

Example:

{
"floorValue": {{floor 3.7}}
}

This rounds 3.7 down to the nearest integer, resulting in:

{
"floorValue": 3
}

Additional Examples:

  • {{floor 5.9}}: Returns 5
  • {{floor (body 'price')}}: Rounds down a price from the request body
  • {{floor (multiply 5.7 3.2)}}: Can be combined with other operators, returns 18
  • {{floor '7.8'}}: Handles string numbers, returns 7

ceil

The ceil operator rounds a number up to the nearest integer. This is useful when you need to remove decimal places and always round upward.

Example:

{
"ceilValue": {{ceil 3.2}}
}

This rounds 3.2 up to the nearest integer, resulting in:

{
"ceilValue": 4
}

Additional Examples:

  • {{ceil 5.1}}: Returns 6
  • {{ceil (body 'quantity')}}: Rounds up a quantity from the request body
  • {{ceil (divide 10 3)}}: Rounds up division results, returns 4
  • {{ceil '3.2'}}: Works with string numbers, returns 4

round

The round operator rounds a number to the nearest integer. It rounds up if the decimal is 0.5 or greater, and rounds down if the decimal is less than 0.5.

Example:

{
"values": {
"roundUp": {{round 3.7}},
"roundDown": {{round 3.2}}
}
}

This rounds the numbers to their nearest integers, resulting in:

{
"values": {
"roundUp": 4,
"roundDown": 3
}
}

Additional Examples:

  • {{round 5.4}}: Returns 5 (rounds down)
  • {{round (body 'score')}}: Rounds a score from the request body to nearest integer
  • {{round (multiply 2.7 1.8)}}: Can be combined with other operators
  • {{round '6.7'}}: Handles string numbers, returns 7

toFixed

The toFixed operator formats a number using fixed-point notation, allowing you to specify the number of decimal places. This is particularly useful for formatting currency or precise decimal values.

Syntax:

{{toFixed number digits}}

Example:

{
"price": {{toFixed 123.4567 2}},
"percentage": {{toFixed 0.12345 3}}
}

This formats the numbers with the specified decimal places, resulting in:

{
"price": "123.46",
"percentage": "0.123"
}

Note: The first argument is the number to format, and the second argument specifies how many decimal places to maintain. The result is rounded to the specified number of decimal places.

Additional Examples:

  • {{toFixed 3.14159 2}}: Returns "3.14"
  • {{toFixed (body 'amount') 2}}: Formats currency amount from request body with 2 decimal places
  • {{toFixed (divide 10 3) 3}}: Formats division result with 3 decimal places, returns "3.333"
  • {{toFixed '123.456789' 4}}: Works with string numbers, returns "123.4568"