TutorialAbsolute number in Gravity Forms calculations

The Gravity Forms Number field supports basic calculations using values from other fields, like add, subtract, divide and multiply, but sometimes we really miss the possibility to convert a negative value to an absolute number (positive value). For example, when we want to calculate the difference between two values. Can this be solved using out of the box Gravity Forms functionality?

The answer is yes… 😉

But it is rather complex, as you need to add at least 4 extra fields with calculations and conditional logic. It is very easy however if you use our Advanced Number Field addon. Here you can just tick the ‘Force Absolute’ box, among some other great advanced number field features:

GravityWP - Advanced Number Field settings detail screenshot
GravityWP – Advanced Number Field settings detail screenshot

Still want to do it the hard way? Buckle up and we will walk you through it step by step.

How to calculate an absolute value in a Gravity Form

We achieve this using a combination of multiple number field, calculations and conditional logic. We will explain it with an example form where the user inputs his (or her) height in meters. Then the form calculates the difference in centimeters. We want this value to be a positive number in any case. We will build a form where you can input your own height in meters. The form then will calculate the absolute difference in centimeters of your height compared to the average woman height, using only Gravity Forms core functionality.

Step by step instruction

Open the form editor to create a new or edit an existing form. Add the following number fields:

  1. The first number field has a fixed value, which is the more or less average height of a woman in meters. We enable calculations and fill in 1.60.
Absolute number instruction, Field 1: insert number field with calculation value of 1.60
Absolute number instruction, Field 1: insert number field with calculation value of 1.60
  1. The second number field is a user input.
Absolute number instruction, Field 2: number field for input of own height
Absolute number instruction, Field 2: number field for input of own height
  1. In the third number field we calculate the difference like Average minus the user input. When your height is above 1.60 meter the result will be a negative value. As we don’t want the user to see this field we hide it in the field visibility settings.
Absolute number instruction, Field 3: number field with calculation of Average minus input
Absolute number instruction, Field 3: number field with calculation of Average minus input
  1. In the fourth field (hidden) we calculate the difference the other way around. When your height is above 1.60 meter the result will be a positive value. Now we have two fields which calculate the different. Now at least one of these contains a positive value. But how to determine which one? We need two more fields for this.
Absolute number instruction, Field 4: number field with calculation of Input minus Average
Absolute number instruction, Field 4: number field with calculation of Input minus Average
  1. Therefore we add another hidden number field, which copies the value of the ‘Average minus input’-field. But we also enable conditional logic for this field and show it only in case ‘Average minus input’ has a value greater than 0.
Absolute number instruction, Field 5: number field with calculation copying the value of Average minus input and conditional logic show if Average minus input > 0
Absolute number instruction, Field 5: number field with calculation copying the value of Average minus input and conditional logic show if Average minus input > 0
  1. And we add yet another hidden number field which copies the value of ‘Input minus average’ which also shows conditionally but now when ‘Input minus average’ has a value greater than 0. Because of this conditional logic the ‘Difference1’ and ‘Difference2’ field will never show at the same time. Note that ‘show’ is not the same as ‘visible’, the field is still invisible to the user as it is hidden in the field settings. In this case ‘show’ means the fields conditional logic evaluates to true and therefore the form will be able to use its value.
Absolute number instruction, Field 6: number field with calculation copying the value of Input minus average and conditional logic show if Input minus average > 0
Absolute number instruction, Field 6: number field with calculation copying the value of Input minus average and conditional logic show if Input minus average > 0
  1. In the last number field we add we will finally be able to calculate the difference as an absolute number. We do this by adding up Difference1 and Difference2. Only one of this fields is shown due to conditional logic. The other one will hold a value of 0, because it is hidden by the conditional logic. Finally we multiply it by 100 to convert centimeters to meters.
Absolute number instruction, Field 7: number field with calculation: (Difference1 + Difference2)/100
Absolute number instruction, Field 7: number field with calculation: (Difference1 + Difference2)/100

As you can see this is quite a lot of work te get an absolute number value in Gravity Forms, and you need quite a lot of fields to achieve this.

Demo form calculating the absolute number

You can try it out here. Click the button to show the fields hidden by the visibility setting. Note Difference1 and Difference2 being shown conditionally.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close