Basic calculations for adding, subtracting, multiplying, and dividing

Prev Next

Overview

The Calculation field allows you to add, subtract, multiply, and divide numeric fields in your forms. This can be useful for calculating price, quantity, or basic scores within your forms.  To get started:

  1. Add Numeric fields to your form in the Form Builder.

  2. Add a Calculation field.

  3. Select Add Calculation in the settings menu on the right.

  4. Select the fields you wish to calculate and enter your desired formula in the Calculation Formula box.

 The following sections describe how to enter each type of calculation formula:  

Add (+)

  1. Follow the steps in the Overview section.

  2. Enter a + sign between the field keys of the two fields you wish to add.

    Note

    Make sure the field keys are surrounded by $ signs.

  3. Press Okay.

    Calculation Formula box showing the Apple and Bread field keys joined with a plus sign

Price of Apple - $Apple_1$
added to
Price of Bread - $Bread_1$

In the Calculation field (labeled Total) we have added the two fields together using the + sign with each fieldkey surrounded by the $ sign.

$Apple_1$ + $Bread_1$

Calculation field settings showing the addition formula with field keys

Note

Since we are calculating a price, we also need to allow for 2 decimal places for each of the Numeric fields (Numeric Settings > Decimals) and the Calculation field (Calculation Settings > Decimals).

Decimal places setting in the Numeric and Calculation field configuration

After saving and publishing the form, we can now go into the FastField App and fill out those two fields. Our Total field will add those two fields together and show the total price.

FastField app showing the Total field displaying the added price

Subtract (-)

  1. Follow the steps in the Overview section.

  2. Enter a - sign between the field keys of the two fields you wish to subtract.

    Note

    Make sure the field keys are surrounded by $ signs.

  3. Press Okay.

Example:

  • In the example below, we have two numeric fields (Price of Bread and Price of Apple) that we wish to subtract.

Calculation Formula box showing the Bread and Apple field keys joined with a minus sign

Price of Bread - $Bread_1$
subtracted from
Price of Apple - $Apple_1$

In the Calculation field (labeled Total) we have subtracted the price of bread from the price of an apple using the - sign. Each fieldkey is surrounded by the $ sign.

$Apple_1$ - $Bread_1$

Calculation field settings showing the subtraction formula with field keys

Note

Because I am calculating a price, I will need to make sure to allow for 2 decimal places in the builder for each of my numeric fields and also for my calculation field. See the picture below.

Decimal places setting in the Numeric and Calculation field configuration

After saving and publishing the form, I will now go into the app and fill out those two fields. My Total field will subtract those fields from each other

FastField app showing the Total field displaying the subtracted price

Multiply (*)

  1. Follow the steps in the Overview section.

  2. Enter a * sign between the field keys of the two fields you wish to multiply together.

    Note

    Make sure the field keys are surrounded by $ signs.

  3. Press Okay.

Example:

  • In the example below, we want to multiply the price of each product by the quantity.

  • We created a Numeric field for each product (Price of Bread, Price of Apple).

  • After each product, we have a Numeric field for the quantity (Quantity).

  • At the end, we have Calculation fields for the total price of each product (Total Price of Bread, Total Price of Apples).

    Note

    We have two Calculation fields to show the total price of each item separately. You could also have just one Calculation field that shows the total of all items together.

Form Builder showing Numeric fields for price and quantity alongside Calculation fields for total price

For review, these are the fields and their associated fieldkeys.

Example: Apples


Price of Apple - $Apple_1$
multiplied by
Quantity - $Qty_Apple$

The calculation inside of your Total Price of Apples will look like this

$Apple_1$ * $Qty_Apple$

Calculation field settings showing the multiplication formula for the apple price and quantity

This is what it looks like in the app after filling it out

FastField app showing the Total Price of Apples field after multiplication

Example: Bread


It will be the same thing for the bread; as follows

Price of Bread - $Bread_1$
multiplied by
Quantity - $Qty_Bread$

The calculation inside of your Total Price of Bread will look like this

$Bread_1$ * $Qty_Bread$

Calculation field settings showing the multiplication formula for the bread price and quantity

This is what it looks like in the app after filling it out

FastField app showing the Total Price of Bread field after multiplication

Divide (/)

  1. In your form, add multiple Numeric fields.

  2. Add a Calculation field that will add the Numeric fields to show their total value (see Add above).

  3. Add another Calculation field that will show the average value and select Add Calculation.

  4. In the Calculation Formula box, enter the field key for the Calculation field that will show the average.

    Note

    Make sure the field key is surrounded by $ signs.

  5. After the field key put a forward slash (/) and the number of Numeric fields that will be divided

    • Example: $Average$ / 3

  6. Press Okay.

Example:

  • In the example below, we will use multiplication, addition, and division to get an average price.

  • For our Numeric fields, we have Price of Bread 1 and Price of Bread 2, each with their own Quantity fields below.

  • First, we want to multiply Total Price of Bread 1 by its Quantity to get the Total Price of Bread 1 (see Multiply above).

  • Then, we will do the same for Total Price of Bread 2.

  • Next, we will add those two totals together to get the Total Price (see Add above).

  • To get the average price, we will create a Calculation field that divides the Total Price and by 2, which is the number of Total Price of Bread fields that we have.

  • This will give us the Average Price of Bread.

  • Below is a screenshot of all of the fields and their names:

Form Builder showing the Numeric and Calculation fields used to calculate an average price

Below are the calculation field names, their fieldkeys in (parenthesis) and their contained calculations follow the = equal sign.

Total Price of Bread 1 (total_bread_1) = $price_bread_1$ * $qty_bread_1$
Total Price of Bread 2 (total_bread_2) = $price_bread_2$ * $qty_bread_2$
Total Price (total_price) = $total_bread_1$ + $total_bread_2$
Average Price of Bread (avg_price_bread) = $total_price$ / 2

Now that the calculations have been added, I can now save and publish my form and make sure everything is calculating correctly. As you can see from the following screenshot, everything looks correct.

FastField app showing the calculated average price after division

Calculation field returning a monetary value

(E.g., if a user enters 1234567, it will appear as $1,234,567.00)  In a normal situation, if you are calculating the values of two fields, the calculation field will return you a value void of commas or the monetary identifier of your choice, ($, £, €, etc.). This is because the calculation field is built to return a value that is ready to be calculated, void of any strings or confusion. If you would like to add this type of eye candy to your end calculation, however, you can follow these steps:  

  1. Highlight everything between the lines, and place it in your calculation field.

  2. Place the fieldkey you are referencing in the "$field_key$" area of the calculation.

  3. If you want to change the monetary indicator from dollars ($) to something else of your choice, then find r = "$" + n.toFixed(2); in the following formula, and replace the $ with the symbol of your choice.

Formula

function formatCurrency(nStr)
{
   nStr += '';
   var x = nStr.split('.');
   var x1 = x[0];
   var x2 = x.length > 1 ? '.' + x[1] : '';
   var rgx = /(\d+)(\d{3})/;
   while (rgx.test(x1)) {
       x1 = x1.replace(rgx, '$1' + ',' + '$2');
   }
   return x1 + x2;
}
n = float($field_key$);
r = "$" + n.toFixed(2);
return formatCurrency(r);

How You Do It

Calculation Formula box with the currency formatting JavaScript function entered

The Result

FastField app showing the calculation field formatted as a currency value