# Using Formulas in Snippets

### Quick Overview Video

At its most basic, formulas can complete static, unchanging math, such as {=10*5} or {=3+3}. Formulas become much more powerful when they interact with other fields within your snippet.

## Using the values of other fields with a formula

### Re-using the value exactly as it was entered

One of the most popular uses of the formula command is to re-use a value that has already been defined through another field. For instance, if a snippet contains a text field that is used for someone’s first name, a formula can display that first name again without needing to retype it:

Hi {formtext: name=firstname}

I can re-use what I entered in as your name above with a formula field: {=firstname}.

I simply create a formula that uses the name of that other field only. I can re-use that value as much as I’d like, so here it is again: {=firstname}.

To easily create formulas that re-use other values, find the Formula option in the list of dynamic commands on the right side of the Text Blaze dashboard. Any fields already in the snippet that have been given a name will be available:

### Modifying the value when re-used

As seen above, I can re-use a value exactly as it was entered into another field. Beyond that, I can use formulas to manipulate and change the original value into something new.

In the example below, the formula command is multiplying whatever number I enter into the formtext field called “amount” by 10:

This snippet’s formula will dynamically calculate that amount for any number entered into the field called amount.

## Performing calculations using formulas

### Using operators for simple calculations

Operators are used to create your formula, and define what the formula does. Which operators go into your formula depends on what you want your formula to do, and what kind of data it’s interacting with. Your formula may contain multiple operators as it gets more complex.

**Math operators** power formulas that interact with numbers. Text Blaze uses the most common operators that you might be familiar with from other applications, like Excel or Google Sheets. Those are +, -, /, *, and ^. Additional details about math operators is available here.

**Comparison and logical operators** allow you to evaluate if a statement is true or false (e.g. 10 > 5). They are primarily used for rules (the if/else command). More information about these types of operators can be found here

### Functions inside Formulas

Text Blaze also includes a wide variety of built-in Functions to use within formulas. A built-in function is a contained piece of functionality designed to perform a particular task as part of a formula.

One example of that might be the *round* function:

Some functions, especially text/string functions, contain a second setting which must be defined when creating the formula. For instance, the *left* function requires indicating how many characters from the left should be retrieved:

### Nesting multiple functions inside a formula

A single formula can contain multiple functions, completing many actions at once. Nesting can be done with both math and text/string functions.

Enter the text you want to be put into uppercase and reversed: {formtext: default=text goes here; name=original}

Here’s that text in uppercase and reversed: {=upper(reverse(original))}

Let’s see how the order of the functions within a formula changes the results:

In this example, there is a square root function nested into a round function. Since the square root function is inside of the round function, that will be calculated first, and then its result will be rounded:

Square root first, then round the results: {=round(sqrt(44.5))}

If the function order is reversed, we receive a different result. Below, 44.5 is rounded first and then the squareroot of the rounded number is calculated:

Round first, square root second: {=sqrt(round(44.5))}

### Combining functions and operators

Functions and operators can be used together inside of a single formula. Simply write the formula using both:

This snippet is a simple division calculator which rounds the result.

Enter the original amount: {formtext: default=48632; name=amount}

Enter how much you’d like to divide that amount by: {formtext: default=7; name=divisor}

Here is the result: {=round(amount/divisor)}

I can also do that with static amounts, such as: {=round(14245/9)}

## Defining variables

As seen throughout this guide, variables are commonly defined by giving a form field a name, and then using that name as a placeholder for its value.

This is seen in the previous example where **”amount”** and **”divisor”** are names applied to text fields, and then those names are used in formulas.

You can also define variables "manually". To create a variable, you’ll need to write it out inside of your snippet. The variable will need a name, and it will need a value.

### Using variables you’ve created

Variables created through assignment mode can be used just like any other variable, by referring to its name in other formulas.

Here I am defining a variable called "target" with the value of 100: {target=100}

Now I am defining a variable called "intro" with some text as the value: {intro="This text will be displayed where I use the intro variable"}

Once defined, I can use them in my snippet like any other variable:

{=3000/target}

{=concat("Hi Text Blaze User. "&intro)}