Gravity Forms Advanced Calculations
Perform advanced calculations with mathematical functions, conditionals, and a powerful formula editor.
What does it do?
Advanced Calculations completely revamps calculations in Gravity Forms. Not only does it empower you with advanced mathematical functions and conditionals, it improves the formula editing experience with a powerful new editor, complete with syntax highlighting, autocompletion, shorthand field references, and so much more!
With Advanced Calculations you can create:
- Mortgage calculators
- Advanced calculated products
- Auto loan calculators
- Investment/Retirement calculators
- BMI calculators
- And many more!
Features
- Heaps of mathematical functions.
Spreadsheet style math directly in your form. - Conditional statements.
Specify multiple formulas in the same field; use the right one based on your conditions with support for numbers and strings. - Formula shorthand.
Simplify your formulas with our field reference shorthand. No more cumbersome merge tags! - Syntax highlighting.
Clarify your complex formulas and spot issues quickly with automatic syntax highlighting. - Secure, tamperproof calculations.
All calculations are recalculated on submission for accuracy and security. - Seamless integration.
Automatically enhances all calculation-enabled fields like Number and Calculated Product fields. - Automatic updates.
Get updates and the latest features right in your dashboard. - Legendary support.
We’re here to help! And we mean it.
Documentation
How do I enable this functionality?
After installing and activating Gravity Forms Advanced Calculations,
Add a Number field to your form. | |
Check Enable Calculation. | |
Insert advanced calculations into form. Use the Formula Cheatsheet for a primer on calculations. |
Feature Details
Merge Tag Shorthand
The true power of calculations with Gravity Forms comes from being able to reference other fields to create calculations from what users have entered. Gravity Forms uses merge tags to reference fields. Merge tags are referenced using the field ID, with or without the descriptor, such as {Field A:1}
or {:1}
.
Gravity Forms Advanced Calculations adds shorthand for fields to clean up your calculations and make it easier to keep track of everything. The shorthand for the same field shown above is written as F1
.
Shorthand offers more readable calculations, especially for complicated functions.
Conditional Statements
Advanced Calculations supports conditional statements such as if/else and if/elseif/else. Use these statements to evaluate calculations based on field values.
if/else
if( F1 > 1.25 && F1 < 5.5 ):
F1 * 50
else:
0
endif;
if/elseif/else
if( F1 < 5.5 ):
F1 * 50
elseif( F1 >= 10 ):
F1 * 25
else:
0
endif;
You may use as many elseif
statements as needed. You are not limited to one.
Supported Operators
The following comparison operators are supported:
==
Equal to!=
Not equal to>
Greater than<
Less than>=
Greater than or equal to<=
Less than or equal to
Comparing String Values
Comparison values can be either a number or a string (e.g. 123
or hello world
).
A few things to note when comparing strings:
- Comparisons are case-sensitive.
hello world
won’t matchHello World
. - Comparisons are not sensitive to whitespace at the beginning and end of strings. Strings are trimmed by Gravity Forms before comparison so a condition like this would match still match a value of
hello
(no whitespace).if ( F1 == ' hello ' ):
- Comparisons can be made with any any single-value text-based field type, including choice-based fields like Drop Down and Radio Button fields.
Multiple Expressions
Conditional statements support multiple expressions within a single condition via the the &&
(and) or the ||
(or) operators.
To require both expressions to be true for the condition to match, you would use the &&
operator. In this example, the condition will only be true if Field #1 is greater than 5 and less than 10.
if ( F1 > 5 && F1 < 10 ):
If you would like your condition to match if any of the conditions are true, you would use the ||
operator. In this example, the condition will return true if Field #1 is greater than 5 or if Field #2 is greater than 5.
if ( F1 > 5 || F2 > 5 ):
While these examples are simple, this functionality is quite robust and can handle conditions that contain expressions with both &&
and ||
operators.
Worth noting that string-based conditions can be combined with number-based conditions.
if ( F1 == 'Package A' && F2 > 100 ):
You can break complex conditionals onto multiple lines for readability.
Condition: Field Has Value
You can easily check if a field has a value like so:
if ( F1 ):
Condition: Field Is Empty
You can check if a field is empty (has no value) like so:
if ( F1 == '' ):
Condition: Field Values Match
You can check if the value of two different fields match like so:
if ( F1 == F2 ):
This works with both numeric and string values.
Syntax Highlighting
Advanced Calculations adds syntax highlighting and autocompletion to the calculation text area. Syntax highlighting drastically improves the readability of calculations, especially complicated ones that use conditional statements and functions.
Autocompletion aids you when inputting a function or conditional statement. When you start typing any supported function, the editor will suggest a completion. Click the suggested function (or hit return) to insert the full function with placeholder values.
The editor also automatically closes parentheses and merge tag braces. Say goodbye to invalid formulas because of an errant parenthesis!
Tooltips
When hovering over shorthand field variables and merge tag field variables with no descriptor in the formula editor, you will see a tooltip displaying the field’s label.
Functions
Advanced Calculations adds support for common functions to both save you time and effort and provide more accurate results.
Absolute Value – abs
Returns the absolute value of a number.
Sample Usage
abs( -78 )
abs( F1 )
Syntax
abs( value )
value
– The value or field of which to return the absolute value.
Example
In the below example, the retuned value is 14
.
abs( -14 )
Arccosine – acos
acos
returns the arccosine of a number in radians.
Sample Usage
acos( 1 )
acos( F2 )
Syntax
acos( value )
value
– The value for which to return the arccosine.
Example
In the below example, the retuned value is 0
.
acos( 1 )
Arcsine – asin
asin
returns the arcsine of a number in radians.
Sample Usage
asin( 1 )
asin( F2 )
Syntax
asin( value )
value
– The value for which to return the arcsine.
Example
In the below example, the retuned value is 1.5707963267948966
.
asin( 1 )
Arctangent – atan
atan
returns the arctangent of a given number in radians.
Sample Usage
atan( 1 )
atan( F2 )
Syntax
atan( value )
value
– The value for which the arctangent should be returned.
Example
In the below example, the retuned value is 0.7853981633974483
.
atan( 1 )
Average – average
Returns the average value of the arguments given.
Sample Usage
average( F1, F2, 26 )
average( 1, 2, 3, 4, F1 )
Syntax
average( value1, [value2, ...] )
value1
– The first value or field to evaluate when calculating the average value.value2, ...
(optional) – Additional values or fields to evaluate when calculating the average value.
Example
In the below example, the returned value is 35
.
average( 12, 58, 35 )
Ceiling – ceil
Rounds a number up to the next largest integer.
Sample Usage
ceil( 25.4 )
ceil( F1 )
Syntax
ceil( value )
value
– The value to round up to the nearest integer.
Example
In the below example, the retuned value is 36
.
ceil( 35.8 )
Cosine – cos
cos
returns a value between -1 and 1 representing the cosine of the given angle in radians.
Sample Usage
cos( 1 )
cos( F2 )
Syntax
cos( value )
value
– The value for which to return the cosine.
Example
In the below example, the retuned value is 0.5403023058681398
.
cos( 1 )
Exponential – exp
Returns e^x, where e is Euler’s number (~2.718) and x is the provided value.
Sample Usage
exp( 25.4 )
exp( F1 )
Syntax
exp( value )
value
– The exponent to raise e to.
Example
In the below example, the retuned value is 148.4131591025766
.
exp( 5 )
Floor – floor
Rounds a number down to the nearest integer.
Sample Usage
floor( 75.763 )
floor( F3 )
Syntax
floor( value )
value
– The value to round down to the nearest integer.
Example
In the below example, the retuned value is 323
.
floor( 323.78 )
Future Value – fv
fv
calculates the future value of an investment based on constant-amount periodic payments and a constant interest rate.
Sample Usage
fv( F1, F2/12, F3, 10000, 1 )
fv( 0.05, 10, F1, F2 )
Syntax
fv( rate, numberOfPeriods, paymentAmount, presentValue, [endOrBeginning] )
rate
– The annualized rate of interest.numberOfPeriods
– The number of payments to be made.paymentAmount
– The amount per period to be paid.presentValue
– The current value of the investment.[endOrBeginning]
– (optional – 0 by default) – Whether payments are due at the end (0) or beginning (1) of each period.
Units must be consistent for rate
, number_of_periods
, and payment_amount
. For example, on a 60 month car loan this is paid monthly, the rate
should be divided by 12 and the numberOfPeriods
is 60. If the same loan was instead paid quarterly, the rate
should be divided by 4 and the numberOfPeriods
is 20.
Example
In the below example, the retuned value is 0.023
.
fv( 0.0311, 60, 450, -25000, 0 )
By default fv
calculates a loan. To use it to calculate a growing investment, add a minus before the formula. For example:
-( fv( 0.08, 5, 300, 2500 ) )
Natural Logarithm – ln
ln
returns the logarithm of a number, base e. e is Euler’s number (~2.718).
Sample Usage
ln( 23 )
ln( F4 )
Syntax
ln( value )
value
– The value to calculate the logarithm, base e.value
must be positive.
Example
In the below example, the retuned value is 6.19644412779452
.
ln( 491 )
Logarithm – log
log
returns the logarithm of a number, base 10.
Sample Usage
log( 7 )
log( F2 )
Syntax
log( value )
value
– The value to calculate the logarithm, base 10.value
must be positive.
Example
In the below example, the retuned value is 1.6901960800285136
.
log( 49 )
Maximum – max
max
returns the largest value in the list of arguments.
Sample Usage
max( F1, F4, 67 )
max( 108, F7 )
Syntax
max( value1, [value2, ...] )
value
– The first value or field to evaluate when calculating the maximum value.[value2, ...]
– (optional) – Additional values or fields to evaluate when calculating the maximum value.
Example
In the below example, the retuned value is 49
.
max( 36, 49 )
Minimum – min
min
returns the smallest value in the list of arguments.
Sample Usage
min( F3, 56, F8 )
min( 3, F1 )
Syntax
min( value1, [value2, ...] )
value
– The first value or field to evaluate when calculating the minimum value.[value2, ...]
– (optional) – Additional values or fields to evaluate when calculating the minimum value.
Example
In the below example, the retuned value is 6
.
min( 18, 6 )
Pi – pi
pi
returns the value of pi to 20 decimal places.
Sample Usage
pi()
Syntax
pi()
Example
In the below example, the retuned value is 3.14159265358979323846
.
pi()
Round – round
round
rounds a number to the nearest integer.
Sample Usage
round( 12.7 )
round( F2 )
Syntax
round( value )
value
– The value to round to the nearest integer.
Example
In the below example, the retuned value is 9
.
round( 8.68 )
Sine – sin
sin
returns a number between -1 and 1 which represents the sine of the given angle in radians.
Sample Usage
sin( 1 )
round( F2 )
Syntax
sin( value )
value
– The value for which to return the sine.
Example
In the below example, the retuned value is 0.8414709848078965
.
sin( 1 )
Square Root – sqrt
sqrt
returns the square root of a number.
Sample Usage
sqrt( F12 )
sqrt( 72 )
Syntax
sqrt( value )
value
– The number to calculate the square root.
Example
In the below example, the retuned value is 12
.
sqrt( 144 )
Tangent – tan
tan
returns a numeric value that represents the tangent of the given angle.
Sample Usage
tan( 1 )
tan( F2 )
Syntax
tan( value )
value
– The value for which to return the tangent.
Example
In the below example, the retuned value is 1.5574077246549023
.
tan( 1 )
Known Limitations
Single and double quotes cannot be escaped in string-based conditions.
Solution: Wrap the string in double quotes if it contains a single quote or single quotes if it contains double quotes.
Example:
'Don't try to escape.'
becomes"Don't try to escape."
When targeting top-level Checkbox fields and Paragraph Text fields in conditional statements, you must use shorthand syntax as Gravity Forms does not listen for these fields in calculations by default.
List fields are not supported in calculations.
Integrations
Invoicing Templates by Gravity PDF
GP Advanced Calculations integrates with Gravity PDF’s Invoicing 2.0+ templates. These templates allow for business-ready invoice generation from Gravity Forms submissions. They’re highly customizable, translatable, support conditional logic, and are GST/VAT compatible. This integration allows for GP Advanced Calculations’ mathematical functions and formulas to be leveraged while working with Gravity PDF’s Invoicing templates.
There’s a variety of sleek templates to choose from and all templates integrate with GP eCommerce Fields, GP Inventory, GP Conditional Pricing, GP Unique ID, and GP Price Range. Learn more about Gravity PDF’s invoicing templates here.
Translations
You can use the free Loco Translate plugin to create translations for any of our Perks. If you’ve never used Loco translate before, here’s a tutorial written for beginners.
FAQs
How can I create a condition based on the label of a selected choice?
Use the :choice_label
merge tag modifier to explicitly get the label from a choice in a formula. (There is also the :label
modifier, but it returns the label of the field rather than the selected choice.)
Example:
if ( {Package:1:choice_label} == 'Premium' ) :
{Options:2} * 0.5
else:
{Options:2} * 0.1
endif;
Hooks
- gpac_functions
- gpac_functions (JS)
- gpac_should_clean_merge_tag_value
- gpac_should_clean_merge_tag_value (JS)
Gravity Forms has hundreds of hooks. Check out our Gravity Forms Hook Reference for the most thorough guide to Gravity Forms’ many actions and filters.
Related Resources
- How to Do Calculations with Gravity Forms
If you’re using Gravity Forms on your WordPress website, you might need an easy way to do Gravity Forms calculations on form fields and user submissions. - Spotlight: Building a Loan Calculator with GP Advanced Calculations
See how James Roberts built a loan calculator for an auto-financing company using GP Advanced Calculations. - Spotlight: Creating Dynamic, Customizable Quoting Forms with Jennifer Erdman
We explore how Jennifer Erdman built a quote calculator — based on highly customizable product offerings — step by step. - Spotlight: Diviguy's Cloud Hosting Cost Calculator
See how Diviguy.com uses Advanced Calculation's conditional formulas to tackle complex monthly cloud hosting pricing logic with ease. - Spotlight: Gasol Foundation Incentivizes Donations with a Tax Break Calculator
See how Jordi Anglada built a tax break calculator to encourage donations for The Gasol Foundation, without knowing how to code.