This doc will help you to build formulas in the WPForms Calculations addon. For additional information and help creating formulas, please see our Calculations addon documentation.
Rules
- Numbers: The dot (.) is the only symbol allowed as a decimal point (e.g., 5.20). Do not use a comma (,) for thousands. (e.g., 12999.45)
- Strings should be wrapped by quotes (‘) or double quotes (“). Examples: ‘Hello, world!’, “My name is Sullie”.
- Adding a semicolon at the end of the formula line is optional.
- Only the operators, variables, conditional statements, and functions listed below are allowed.
Arithmetic
| Operator | Description |
|---|---|
| + | Add |
| – | Subtract |
| * | Multiply |
| / | Divide |
| ( ) | Parentheses; enclose parts of the formula to be calculated first |
| % | Modulo; returns the remainder of a division |
Field Variables
Use values from other fields in your calculations.
Supported field types: Single Line Text, Paragraph Text, Dropdown, Multiple Choice, Checkboxes, Number, Name, Email, Number Slider, Phone, Address, Date / Time, Website / URL , Rating, Hidden, Checkbox Items, Multiple Items, Dropdown Items, Single Item, Total
| Variable Pattern | Variable Example | Explanation | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| $FX | $F1 | Value of the field with ID #X (#1). All allowed fields provide the main value of the field. A simple field provides the value of the input element. The combined fields, like Name (but not a simple one), Address, Checkboxes, etc., will have the combined value. For example the Name field, his combined value is [first last] “John Smith”. Please note: the combined value stored in $FX has the same formatting as the main field entry value visible on the Entry View page. | ||||||||||
| $FX_subfield | $F1_first | The value of the subfield of the field with ID #X. This applies to the combined fields:
| ||||||||||
| $FX_n | $F1_n | The value of the choice n of the checkbox field with ID #X. The value:
This applies only to the Checkboxes field and Payment Checkbox Items field. If Dynamic Choices are enabled, choice variables will not be available. | ||||||||||
| $FX_amount | $F12_amount | The amount value of the payment field with ID #X. Always a number, like 9.99. This applies to all the Payment fields that can be used in calculations: payment-checkbox, payment-multiple, payment-select, payment-single, payment-total |
Conditional Statements
Use conditional statements to specify different formulas to be used when certain conditions are met.
if/else
if ( $F1 > 10 ):
$F1 * 50
else:
0
endif;
if/elseif/else
if ( $F1 < 5.2 ):
$F1 * 50
elseif ( $F1 >= 16 ):
F1 * 25
else:
0
endif;
Tip
You may use as many elseif statements as needed.
Logical Operators
| Operator | Description |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
Conditional Operators
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Functions
Math Functions
| Function/Usage | Description |
|---|---|
| abs( value ) | Returns the absolute value of a number |
| average( value1, [value2, …] ) | Returns the average value in args |
| ceil( value ) | Rounds number up to the next largest integer |
| exp ( value ) | Returns e^x, where e is Euler’s numbers and x is the provided argument |
| floor( value ) | Returns the largest integer less than or equal to a number |
| ln( value ) | Returns the logarithm of a number, base e |
| log( value ) | Returns the logarithm of a number, base 10 |
| max( value1, [value2, …] ) | Returns the largest value in the list of arguments |
| min( value1, [value2, …] ) | Returns the smallest value in the list of arguments |
| pi() | Returns pi (π) to 20 decimal places |
| pow( base, exponent ) | Returns base raised to the power of exponent |
| rand( min, max ) | Generates a random integer |
| round( value, precision ) | Rounds a number to the nearest integer |
| sqrt( value ) | Returns the square root of a number |
String Functions
| Function/Usage | Description |
|---|---|
| num( string, precision = 14 ) | Converts string to a number.
If precision is provided, the result value will be rounded to the given number of digits after the decimal point. |
| trim( string ) | Strips whitespace (or other characters) from the beginning and end of the string |
| truncate( string, length ) | Returns the first length characters of the string |
| concat( str1, [str2, …] ) | Concatenates all arguments str1, str2 … strN to one string |
| join( separator, str1, [str2, …] ) | Join arguments str1, str2 … strN to one string using the seperator |
| format_amount( amount ) | Format the amount with a currency symbol |
Date/ Time Functions
| Function/Usage | Description |
|---|---|
| now( format = ‘ ‘ ) | Returns the current date and time. The format is the WPForms datetime string format. An empty string (default value) means default format: d-m-y H:i. |
| date_diff( start, end, units = ‘days’, format = ‘ ‘ ) | Calculate time range length in units. Units: years, months, weeks, days, hours, minutes, seconds. The format is the WPForms datetime string format. An empty string means that we will try to determine the right format automatically. |
| years( start, end, format = ‘ ‘ ) | Calculate time range length in years. The format is the WPForms datetime string format. An empty string means that we will try to determine the right format automatically. |
| months(), weeks(), days(), hours(), minutes(), seconds() functions | Calculate time range length. Similar to years() above. |
Other Functions
| Function/Usage | Description |
|---|---|
| debug( value1, [value2, … ] ) | Outputs debug data to debug log on the server and to browser console when executed client side. |