Creating Formulas
Written by the Divi Engine Documentation Team
Introduction
This documentation outlines the creation of mathematical formulas within Divi Form Builder Calculable Fields. The system utilizes MathExecutor for server-side calculations and JavaScript eval for frontend calculations. Dynamic variables from form fields are formatted using the syntax %%form_field_id%%
.
The formula can be added within the Calculation Field settings:
Basic Operators (Server-Side & Frontend)
You can use the following functions and operators in your formulas:
- Addition:
+
- Example:
%%field_1_id%% + %%field_2_id%%
- Example:
- Multiplication:
*
- Example:
%%field_1_id%% * %%field_2_id%%
- Example:
- Division:
/
- Example:
%%field_1_id%% / %%field_2_id%%
- Example:
- Subtraction:
-
- Example:
%%field_1_id%% - %%field_2_id%%
- Example:
- Parentheses:
()
- Example:
(%%field_1_id%% + %%field_2_id%%) * 3
- Example:
Form Field Values
To incorporate form field values into your formulas, use the %%form_field_id%%
syntax. Replace form_field_id
with the actual ID of the form field you want to reference.
Advanced Server Side Calculation
Server Side calculation provides additional functions that you can use in your formulas:
- Average (avg):
avg(x, y, ...)
- Calculates the average of the specified values.
- Round (round):
round(x, decimalPlaces)
- Rounds the specified value to the given number of decimal places.
Examples with Functions
Average Calculation
- Formula:
avg(%%quiz_score_1%%, %%quiz_score_2%%, %%quiz_score_3%%)
- Explanation: Calculates the average quiz score from three form fields.
Rounded Total
- Formula:
round(%%total_amount%% / %%installments%%, 2)
- Explanation: Calculates and rounds the average amount per installment to two decimal places.
Addition
- Formula:
%%quantity%% + avg(%%price_1%%, %%price_2%%, %%price_3%%)
- Explanation: Adds the quantity to the average of three price values.
Multiplication
- Formula:
round(%%quantity%% * %%unit_price%%, 2)
- Explanation: Multiplies the quantity by the unit price and rounds the result to two decimal places.
Division
- Formula:
%%total_amount%% / %%installments%%
- Explanation: Divides the total amount by the number of installments.
Subtraction
- Formula:
%%initial_value%% - %%discount%%
- Explanation: Subtracts the discount from the initial value.
%%initial_value%% - %%discount%%
Complex Formula
- Formula:
round(avg(%%field_1_id%%, %%field_2_id%%) * 3, 1)
- Explanation: Calculates the average of 'field_1_id' and 'field_2_id', multiplies the result by 3, and rounds to one decimal place.
Advanced Frontend Calculation
Frontend calculation alows offers a number of advanced functions/utilities however these differ to the server-side options. We have outlined a number of examples below for you.
- Average (avg):
avg(x, y, ...)
- Calculates the average of the specified values.
- Round (round):
round(x, decimalPlaces)
- Rounds the specified value to the given number of decimal places.
Examples with Functions
Average Calculation
- Formula:
avg(%%quiz_score_1%%, %%quiz_score_2%%, %%quiz_score_3%%)
- Explanation: Calculates the average quiz score from three form fields.
Rounded Total
- Formula:
round(%%total_amount%% / %%installments%%, 2)
- Explanation: Calculates and rounds the average amount per installment to two decimal places.
Addition
- Formula:
%%quantity%% + avg(%%price_1%%, %%price_2%%, %%price_3%%)
- Explanation: Adds the quantity to the average of three price values.
Multiplication
- Formula:
round(%%quantity%% * %%unit_price%%, 2)
- Explanation: Multiplies the quantity by the unit price and rounds the result to two decimal places.
Division
- Formula:
%%total_amount%% / %%installments%%
- Explanation: Divides the total amount by the number of installments.
Subtraction
- Formula:
%%initial_value%% - %%discount%%
- Explanation: Subtracts the discount from the initial value.
%%initial_value%% - %%discount%%
Complex Formula
- Formula:
round(avg(%%field_1_id%%, %%field_2_id%%) * 3, 1)
- Explanation: Calculates the average of 'field_1_id' and 'field_2_id', multiplies the result by 3, and rounds to one decimal place.