# Formula

Using the **Formula** (formerly Total) element, you can build any formula for your Calculator. For that, you should add available **Fields** and **Operators** by clicking on them.  Watch this in-depth video on how to use the Formula element.

{% embed url="<https://youtu.be/IVHojREZsGg?feature=shared>" %}

## Element Section

The **Formula** element contains the following attributes in the Element section:

* **Title** - Give the description of the total calculation
* **Calculator Formula area** - Create your own formula for the calculator
* **Available Operators** - This field contains mathematical operators and functions that can be used in the formula
* **Available Fields** - This field contains all elements as colored letters of the created calculator form with their ordered IDs.

![](/files/RVXOdpRMb6TXAI1Ntdla)

## Settings Section

And it has the following attributes in the Settings section:

<figure><img src="/files/2iVGRqi1mEufT5MrgYtl" alt=""><figcaption></figcaption></figure>

* **Show the legacy formula view** - switches the mode of the formula editor to the legacy (old) view
* **Hidden by Default** - the element will be hidden until a corresponding [condition ](/cost-calculator-builder/conditional-system/introduction.md)is met
* **Calculate hidden by default** - if any case hides the element: via the **Hidden by Default** setting or  hidden by some [**condition**](/cost-calculator-builder/conditional-system/introduction.md), the element's default value (if it is set) will be counted in the formula&#x20;
* **Additional Classes** - add [extra classes](/cost-calculator-builder/plugin-features/additional-classes.md) to the HTML elements to further style changes

{% hint style="info" %}
If the Formula field is **hidden** in the calculator using the **Hidden by Default** setting, it **will not be displayed** in checkouts, emails, [orders](/cost-calculator-builder/pro-plugin-features/orders.md), or [PDFs](/cost-calculator-builder/cost-calculator-settings/global-settings/pdf-entries.md) as well.
{% endhint %}

### Add a measuring Unit

This setting allows you to add a measuring unit (Ω, cm, kg, inch, etc.) to the field.

<figure><img src="/files/2eZu8wwbW9qSrsvg8aFz" alt=""><figcaption><p>Add a measuring unit</p></figcaption></figure>

* **Unit Symbol** - enter the unit symbol&#x20;
* **Position** - select the position where the symbol should be displayed relative to the number
* **Thousands separator** - select the the symbol to [separate thousands ](#user-content-fn-1)[^1]
* **Number of decimals** - indicate the number of decimals in the fractional part
* **Decimal separator** - select the symbol to separate fractional part

## Available Fields

Fields in Formula are shown as green letters. They are shown in alphabetical order based on their ID.&#x20;

<figure><img src="/files/yFPFSjgOSSdmvHtWmxIu" alt=""><figcaption></figcaption></figure>

If you delete any field, the letters will stay the same:

<figure><img src="/files/0RXWXOOHs1zoMQDhTdqZ" alt=""><figcaption></figcaption></figure>

## **Available Operators**

You can see these available operators in the Formula element.

**Basic Arithmetic Operators:**&#x20;

* **Addition (+)**: Combines two numbers to produce their sum. For example, 5 + 3 equals 8.
* **Subtraction (-):** Subtracts one number from another to yield the difference. If you subtract 3 from 8, you get 5.
* **Division (/)**: Divides one number by another, resulting in the quotient. When you divide 10 by 2, the result is 5.
* **Multiplication (\*)**: Multiplies two numbers, giving the product. 4 multiplied by 6 equals 24.
* **Modulo(%)**: It finds the remainder when one number is divided by another. 25%6=1 (1 - modulo).

**Mathematical Methods:**

* **round(x)**: This method returns the value of x rounded to the nearest integer. For instance, round(4.7) is equal to 5.
* **^pow(x, y)**: Calculates x raised to the power of y. If you evaluate pow(8, 2), you'll get 64.
* **√ sqrt(x)**: Computes the square root of x. The square root of 25 is sqrt(25) = 5.
* **abs(x)**: Yields the absolute (positive) value of x. For example, abs(-3.7) equals 3.7.
* **ceil(x)**: Rounds up x to the nearest integer. If you apply ceil(4.4), the result is 5.
* **floor(x)**: Rounds down x to the nearest integer. When you use floor(8.7), the outcome is 8.

**Conditional Operators**

Conditional operators are vital tools for making decisions based on conditions. They allow you to control the flow of your calculations.

* **IF**: This is a fundamental conditional keyword. It enables you to execute a block of code if a specified condition is true. For example: if (x > 10) { }
* **IF ELSE:** Extending the 'if' statement, 'if else' lets you define an alternative block of code to execute if the initial condition is false. It provides a way to branch your calculations based on different scenarios. For example: if (x > 10) { } else { }

**Logical Operators**

Logical operators are used to combine and manipulate conditions.

* AND: Combines two conditions and evaluates to true only if both conditions are true.
* OR: Combines two conditions and evaluates to true if at least one condition is true.
* Less than (<) and Greater than (>): These operators compare two values and return true if the specified condition holds.
* Not equal to (!=) and Equal to (==): These operators are used to test the equality or inequality of two values.

## Formula Functions: MIN and MAX

The Cost Calculator Pro plugin includes several powerful functions that you can use within formulas to create dynamic and responsive calculations. Two of these functions are `MIN` and `MAX`, which are used to determine the smallest and largest numbers from a set of values, respectively.

#### MIN Function

The `MIN` function is used to find the smallest number in a given set of values. This can be useful in scenarios where you need to determine the minimum value among several options.

**Syntax**

```scss
 MIN(value1, value2, ..., valueN)
```

**Example**

Suppose you have three numbers: 7, 13, and 21. Using the `MIN` function, you can find the smallest number among them:

```scss
 MIN(7, 13, 21)
```

In this case, the function will return `7`, as it is the smallest number in the set.

#### MAX Function

The `MAX` function is used to find the largest number in a given set of values. This is useful when you need to determine the maximum value among several options.

**Syntax**

```scss
 MAX(value1, value2, ..., valueN)
```

**Example**

Suppose you have three numbers: 7, 13, and 21. Using the `MAX` function, you can find the largest number among them:

```scss
 MAX(7, 13, 21)
```

In this case, the function will return `21`, as it is the largest number in the set.

#### Practical Applications

**Using MIN and MAX in a Cost Calculator**

These functions can be particularly useful in cost calculations where you need to set limits or find optimal values. For example, if you have different pricing options based on quantity and you want to ensure the final cost reflects the minimum or maximum price, you can use `MIN` or `MAX` accordingly.

**Example 1: Minimum Price Selection**

You offer three different prices for a product based on the supplier. You want to use the lowest price available:

```scss
MIN(price_supplier1, price_supplier2, price_supplier3)
```

**Example 2: Maximum Discount Selection**

You have several discount options and you want to apply the largest discount available:

```scss
MAX(discount_option1, discount_option2, discount_option3)
```

#### Conclusion

The `MIN` and `MAX` functions in the Cost Calculator Pro plugin are essential tools for creating flexible and accurate calculations. By understanding how to use these functions, you can enhance your cost calculations and ensure they meet your specific needs.

{% hint style="info" %}
You can review the mathematical methods [here ](https://www.w3schools.com/js/js_math.asp)to get more information. Note: All these methods are written with a **Math prefix** since they are objects inherited from the Math library.
{% endhint %}

## **Showing Formula Element**

Let's see the example of the **Formula** element:

![](/files/A1Qf2Jp3bZpjtXxT8dkU)

You can see numbers at the beginning of each line. This is to make creating long formulas much easier.&#x20;

{% hint style="info" %}
You can undo the last changes in the formula editor using the **Ctrl/CMD+Z** keys on their keyboard. It is also possible to select and copy a part of the formula with the cursor/mouse and select the whole via **Ctrl/CMD+A** or **Ctrl/CMD+C** keys on the keyboard.
{% endhint %}

Creating formulas in the total element using available operators and fields is not the only thing you can do with it. Below we will discuss this formula element’s condition features in more in-depth.

## IF/ IF Else Statement

You can use conditions using the IF and IF ELSE statements. These tools help you handle various situations and set specific rules for your calculations.

#### **IF Statements**

The IF statement lets you tell the calculator what to do if a particular condition is met. For instance, you can use it to calculate discounts only if the total amount is greater than a certain value.

**Example**: IF(total > 100 \*0.9 \*1)

In this case, if the total is more than 100, a 10% discount will be applied; otherwise, it will display the value with no discount.

#### **IF ELSE Statement**

The IF ELSE statement adds another layer of logic. It allows you to set an alternative course of action if the initial condition isn't met.

**Example**: IF(total > 500 \*0.1 \*0.125)

Here, if the total is over 500, the calculator will multiply the value by 0.1; otherwise, it will multiply by 0.125.

## **Practical Example: Loan Calculator**

Now, let's see the Loan Calculator which is built with a quantity, two range buttons, and total elements as a sample example. Here is the standard formula for calculating the loan amount:

![](/files/HNxSX6KPSd4j6dibOCtZ)

And now we will create an if/else statement so that the total sum of a loan calculator should be higher than or equal to 500 like below:

![](/files/A1Qf2Jp3bZpjtXxT8dkU)

So, according to the formula above, the loan amount will be always higher than or equal to 500. Please check the following pictures:

The result of the **if statement**:

![](/files/w0v2ixDv2vOUyhpgTIF9)

The result of the **else statement:**

![](/files/MsozQf1ZqjZ2nMe3ewyB)

{% hint style="info" %}
If you want to apply a separate if/else statement for each field and set a separate Total element for each statement. We recommend you to use the Summary Total formula to get the sum of all Total elements and to avoid a complex calculation process.
{% endhint %}

In the following example, different if/else statements were used for each Total element. And the Summary Total formula calculates the overall sum of these two Total elements:

![](/files/8ELKZCffkhahc4ZMr1Ov)

## Alternative Symbol

The Alternative Symbol option is available in order to display multiple Total fields with different calculations and symbols.

![](/files/FOOqh3ByOWvxeUtyPn88)

{% hint style="info" %}
The inserted symbol in the Alternate Symbol field takes precedence over the currency symbol in the [Settings](/cost-calculator-builder/cost-calculator-settings/calculator-settings.md#currency) menu.
{% endhint %}

The Alternative Symbol is displayed after pasting the symbol to the appropriate field.

![](/files/6VXWn95ZFKY93GONdYoz)

**The result:**

![](/files/D84lMF3268MXSHpHA3jW)

{% hint style="info" %}
If you are using the currency symbol in the Alternative Symbol field and want to integrate the Calculator with WooCommerce, then you have to consider whether WooCommerce supports that currency.
{% endhint %}

## Comparing the Legacy and New Formula View

**Legacy Formula View**

The Old Formula looked like this:

<figure><img src="/files/XCXyHfkWvov7SPvbMr2e" alt=""><figcaption></figcaption></figure>

There were long words for fields with  ID. The element used to have limited mathematical operators. To put conditions if/ if else users needed to use a formula in Javascript style.

**New Formula View**

Now, the New Formula looks like this:

<figure><img src="/files/A1Qf2Jp3bZpjtXxT8dkU" alt=""><figcaption></figcaption></figure>

The view has become more user-friendly. There are green letters instead of long-tail words. There are more available operators, such as !=, = =, < >, etc. Now, users can add conditions with IF/ IF ELSE statements easily. It is possible to add letters and operators from the keyboard. When putting any letters not related to fields, they will be shown grey. There are lines to make creating long formulas much easier. Users can undo the last changes in the formula editor using the Ctrl/CMD+Z keys on their keyboard. It is possible to select and copy a part of the formula with the cursor/mouse and select the whole via Ctrl/CMD+A or Ctrl/CMD+C keys on the keyboard.

[^1]: r.g 7, 000, 000 (seven millions)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.stylemixthemes.com/cost-calculator-builder/calculator-elements/total.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
