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.
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.
- 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.
- decimal - number of decimal places.
- dec_point - character used for decimal 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. ) 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=" * " 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]
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).
You can read more about this approach at https://www.mathsisfun.com/operation-order-pemdas.html
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= type=total] + [frm-stats id=150 151= type=total] + [frm-stats id=160 165= 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]
Much appreciation to Andrew Klimek
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!