Use the frm-math shortcode to calculate the value of a math expression. Place the math expression inside the frm-math shortcode and the result of the calculation will be displayed. For example, this will display the result of subtracting one stat from another:
[frm-math] [frm-stats id=67 type=total] - [frm-stats id=42 type=total] [/frm-math]
This shortcode is used with static values and values that have already been saved in a form. If you are looking for calculations that change while the form is filled out, see form field calculations.
Values
The math expression can contain numbers and other shortcodes (like the frm-stats shortcode). In a View, email, or success message, field shortcodes like [100] can also be included.
Operators
A math expression can use addition (+), subtraction (-), multiplication (*), mod (%), and division (/). Parentheses can also be included. They can be nested, e.g.
[frm-math] (1 + (3 * 3)) / 2 [/frm-math] Result: 5
Where can the math functions be used?
The frm-math shortcode can be used in a View, directly on a page, in an email, as a default value, or anywhere else that WordPress shortcodes are processed.
Parameters
- error - the content to display if there's an error in the math expression.
- error=debug - The math expression will be displayed, as it appears after shortcodes inside it are processed. You can use this to help determine why the error occurred. Only use error=debug during development and testing, not when the content is live.
- error="content of your choosing" - The value set for the error parameter will be displayed. Suggested values: a default number (e.g. 0), a message, or nothing. The default is to display nothing.
Read more about math errors.
- clean - Determines whether the math expression should be cleaned of non-math characters.
- clean=1 - All content that doesn't belong in a math formula, like text, punctuation marks, and currency symbols, is removed. This removal happens after shortcodes have been processed, just before the math expression is evaluated.
- clean=0 - The math expression remains as it is after shortcodes have been processed. This is the default.
- thousands_sep - character used to separate thousands. Example: thousands_sep='.'
- decimal - number of decimal places to round. Example: decimal=2
- dec_point - character used for decimal point. Example: dec_point='.'
Using math in a conditional
If you'd like to display content conditionally based on the result of a frm-math calculation, you can use the following approaches.
Use math directly in a conditional
If you want to display the content in a View and your math expression has only field shortcodes (e.g. [100]) and set numbers (e.g. 10), you can use the frm-math shortcode directly with the frm-condition shortcode. Include source=frm-math and put the math expression in the content param. For example, to multiply two field values, use the following approach:
[frm-condition source=frm-math content="[100] * [102]" greater_than=10]content if the result is greater than 10[/frm-condition]
Save the math value in a param
For math expressions that use standard WordPress shortcodes, like frm-stats and frm-field-value, you can use frm-set-get to save the result of frm-math in a param. Then you can use the param with frm-condition.
For example, an event has space for 100 people. The stat for the number of spaces already taken is: [frm-stats id=18 type=count].
You can calculate the remaining spaces for the event and save the value in a parameter:
[frm-set-get param=remaining_spaces] [frm-math] 100 - [frm-stats id=18 type=count] [/frm-math] [/frm-set-get]
You can then show content conditionally based on the number of remaining spaces.
[frm-condition source=param param=remaining_spaces greater_than=0 less_than=10] If you want to attend, sign up now! There are fewer than ten spaces remaining.[/frm-condition]
[frm-condition source=param param=remaining_spaces less_than_or_equal_to=0]Sorry! We're fully booked this year. We hope you can join us next year.[/frm-condition]
Errors
If the content inside the frm-math shortcode isn't a proper math expression, the value in the error param will be displayed.
Any of the following in the math expression will cause an error:
- letters, punctuation (other than commas and periods), or other non-math characters, like $
- unmatched parens
- missing or empty values, e.g. 3 + * 2
Note: Empty content isn't considered an error.
How the math is evaluated
The math expression is evaluated following a standard PEMDAS style approach:
- parentheses are evaluated first,
- then multiplication, mod, and division (left to right),
- then addition and subtraction (left to right).
Examples
Average test score
In a View, show the average test score of each student by adding the scores from three different forms and dividing by three. The student id in the form used for the view is in field 42. Fields 100, 150, and 165 hold the student test scores. Fields 101, 151, and 165 hold the student ids. Each of the three forms limits entries to one per user.
[frm-math] ([frm-stats id=100 101=[42] type=total] + [frm-stats id=150 151=[42] type=total] + [frm-stats id=160 165=[42] type=total])/3 [/frm-math]
Stat with filters combined with OR
The stats shortcode can give you the number of gold members and, separately, the number of bronze members. If you want the total number of members who have either the gold or bronze level, you can use the math shortcode to combine the two stats. Field 25 holds the membership level.
[frm-math] [frm-stats id=25 25="bronze" type=count] + [frm-stats id=25 25="gold" type=count] [/frm-math]
Show content if a user filled out one of two forms
Show content if a user has filled out at least one of two forms. Field 70 is a field in Form A, and field 80 is a field in Form B.
First, use math to determine how many times the user has filled out both forms and save the result in a param.
[frm-set-get param="forms_total"] [frm-math] [frm-stats id=70 user_id=current type=count] + [frm-stats id=80 user_id=current type=count] [/frm-math] [/frm-set-get]
Then use the result in a conditional.
[frm-condition source=param param=forms_total greater_than=0] content you want to display [/frm-condition]
Show incremental content
In the content box in a view, you may show incremental content. This example will show the content on every 3rd entry show in the view. This count resets each time the page is changed.
[frm-condition source=frm-math content="[entry_position] % 3" equals="0"]This is the incremental content[/frm-condition]
Add values from Views which may have no entries
You may want to combine the values of several Views, each of which displays one number, which may also display the no entries message. Since frm-math requires a properly formatted math expression, it wouldn't work properly with a View that displays the no entries message. To get around this, you can use frm-condition to only display Views when they have values.
- Begin your math expression with a value. It could be a field value, a fixed value (like 100), or a shortcode that results in a value. If you don't have anything like that naturally, you would use 0.
- In each View, put "none" in the no entries message box.
- For each View you want to add, create a conditional to test if the value of the View is not like "none". Put + and the View shortcode inside frm-condition.
[frm-condition source=display-frm-data id=42 not_like="none"] + [display-frm-data id=42 filter=limited][/frm-condition]
- The full math expression for adding two Views could look something like this:
[frm-math] 0 [frm-condition source=display-frm-data id=42 not_like="none"] + [display-frm-data id=42 filter=limited][/frm-condition][frm-condition source=display-frm-data id=36 not_like="none"] + [display-frm-data id=36 filter=limited][/frm-condition][/frm-math]
Add values submitted in Lookup checkbox fields
If you have set up multiselect cascading fields using Lookup checkboxes, you may want to calculate the total of the values submitted and pass the total to a payment form. To get started, you can use the following approach:
- Create a Single Entry View.
- Add a filter in the Advanced Settings of your View that says Entry key is equal to [get param=pass_entry].
- In the Listing Page Content box, add the following math shortcode.
[frm-math][100 sep=" + "][/frm-math]
- Save the View then publish it on a page. Copy the URL of the page.
- Go to your form Settings → Actions & Notifications. Click on the Confirmation form action.
- Select the Redirect to the URL button. In the Redirect URL box, insert the URL of the page where your View is published, followed by ?pass_entry=[key]. Note: Leave the word [key] as it is.
- In your Payment form, look for the field where you want to store the payment amount, then add the View shortcode you created in Step 4. Don't include 'filter=limited' to the View shortcode.
- Click Update to save your form.
Show number as currency in a View
In the Content box in a View, you may show the number as currency. The easiest way to format a value as currency, is to use [100 format=currency].
If you need more customization, the frm-math shortcode will help. This example will add a currency symbol and include up to two decimal places in the displayed number. Your shortcode should look something like this:
$ [frm-math decimal=2] [100] [/frm-math]
Where 100 is the number field ID. Replace $ with your preferred currency symbol.
Show separate value as currency in a View
If you are adding prices in your Saved Value, you may want to display the number as a currency. To get started, you can use the following approach:
- Enable separate values for your checkbox, radio button, or dropdown field.
- For each Saved Value, set the text to something like "black shirt 20". Don't use any punctuation, parentheses, dashes, asterisks, slashes, plus signs, percentage signs, or any symbol that could be used in a math expression.
- Create a View. In the Content box, you may use this shortcode that will add a currency symbol and include up to two decimal places in the displayed number. Your shortcode should look something like this:
$ [frm-math clean=1 decimal=2] [100 show=value] [/frm-math]
- Where clean=1 param will remove all non-math characters, including the letters.
- Replace 100 is the checkbox, radio button, or dropdown field ID using separate values.
- Replace $ with your preferred currency symbol.
Show separate incremental counts for each user
If your users submit a single form multiple times, you may want to assign an incremental number counter to the form every time a logged-in user fills it in. To get started, you can use the following approach:
- Enable front-editing of entries for logged-in users.
- Add a hidden field to your form with this shortcode as a default value.
[frm-math] [frm-condition source=frm-field-value field_id=631] [frm-field-value field_id=631 user_id=current] + [/frm-condition] 1 [/frm-math]
Replace 631 for the id of the same field, the field that has this frm-math shortcode in it.
- It will grab the latest count from the logged-in user in the form (if there is one) and add one to it. By default, it will start with 1.
Note: A limitation with this is that if a user opens up the same form more than once at the same time, they will all get the same number. To avoid this, you could use custom code with the frm_validate_field_entry hook.
Credits
The clever idea of creating a math shortcode came from Andrew Klimek. The code he kindly shared was the inspiration for the frm-math shortcode. Many thanks, Andrew!