Help:Expression and If expression parser

From Ballotpedia
Jump to: navigation, search

Expression parsers allow you to perform mathematical and logical equations using data either input into a wiki page or obtained through a dpl.

Operators

Operation CodeExample
Mathematical expressions
Addition{{#expr: 2 + 2 }} 4
Subtraction{{#expr: 2 - 2 }} 0
Multiplication{{#expr: 2*5 }} 10
Division{{#expr: 10 / 2 }} 5
Parenthesis{{#expr: 10 - 5 / 2 }} versus {{#expr: (10-5) / 2}} 7.5 versus 2.5
Logical Statements
Greater Than{{#expr: 5 > 3 }} 1
Equal to{{#expr: 5 = 3 }} 0
Less Than{{#expr: 5 < 3 }} 0
Logic{{#expr: 5 and 3 }} or {{#expr: 5 or 3 }} 1 or 1
Rounding{{#expr: 9.8756 round2 }} 9.88
[1]

Notes on expressions

  • Numbers entered within expression commands should contain no punctuation aside from a decimal point. Commas will break the parser.
  • If you are pulling a number with commas that you would like to eliminate, the Format Number parser works. Use, {{#formatnum: 54,385|R}} --> {{#formatnum: 54,385|R}}.
  • Logical statements evaluate as true and false, where 1 = true and 0 = false. These responses can be modified with a simple If parser if needed or nested in a If-Expression parser.

If-Expression parser

The If-Expression parser allows you to create logical statements about mathematical functions that will render certain results if true and other results if false. Similar to the IfEq parser, it is useful for making changes to templates and dpl's based on numerical results.



If-Expression example:

The code reads:
{{#ifexpr: A MATHEMATICAL EXPRESSION INVOLVE "AND, OR, =, <, >" | WHAT WILL RENDER IF TRUE | WHAT WILL RENDER IF FALSE }}



The template code reads:
{{#ifexpr: (5+4) < 10 | Hello World | Good bye world }}



The result:
Hello World

Notes on If-Expressions

  • Example {{#ifexpr: {{NUMBEROFACTIVEUSERS}} >15 |Active Wiki| Dead Wiki}} -->Active Wiki
  • IfEq parsers can be used to change style edits in a table based on numbers.

References

Frequently asked questions

Question:  Why does my page look like this?
Expression error: Unrecognized punctuation character ",".
Response:  
This is a result of improper number formatting. Many fixed variables carry commas in their autmoatic formatting. Look at your code and use {{formatnum:NUMBER}}(Help:Format Number) to eliminate any commas automatically.