Jump to content

Help:Calculation

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by Patrick (talk | contribs) at 09:28, 20 April 2006 (→‎See also: *{{tim|csn}} - separates digits of an integer in groups of three.). It may differ significantly from the current version.

With the WikiMedia extension ParserFunctions one can do computations.

The expr function computes mathematical expressions. The syntax is:

{{ #expr: expression }}

The spaces are not needed. Inside numbers no spaces are allowed.

The supported operators (roughly in order of precedence) are:

Operator Operation Example
none {{#expr: 123456789012345}} = 1.2345678901234E+14
{{#expr: 0.000001}} = 1.0E-6
* Multiplication {{#expr: 30 * 7}} = 210
/ or div Division {{#expr: 30 / 7}} = 4.2857142857143
{{#expr: 30 div 7}} = 4.2857142857143
+ Addition {{#expr: 30 + 7}} = 37
- Subtraction (or negation) {{#expr: 30 - 7}} = 23
mod Modulo, gives the remainder of a division {{#expr: 30 mod 7}} = 2
round Rounds off the number on the left to the specified number
of digits after the decimal place, given on the right
{{#expr: 30 / 7 round 7}} = 4.2857143
Rounds off the number on the left to the specified power
of 10, given on the right
{{#expr: 1234567 round -3}} = 1235000
= Equality {{#expr: 30 = 7}} = 0
<> or != Inequality {{#expr: 30 <> 7}} = 1
< Less than {{#expr: 30 < 7}} = 0
> Greater than {{#expr: 30 > 7}} = 1
<= Less than or equal to {{#expr: 30 <= 7}} = 0
>= Greater than or equal to {{#expr: 30 >= 7}} = 1
and Logical AND {{#expr: 30 and 7}} = 1
or Logical OR {{#expr: 30 or 7}} = 1
not Logical NOT {{#expr: not 7}} = 0
{{#expr: not 0}} = 1
( ) Grouping operators {{#expr: (30 + 7) * 7 }} = 259

The boolean operators consider 0 to be false and any other number to be true. As output, "true" produces 1.

Numbers

In the numbers in submitted expressions a decimal point is allowed, and a non-integer result has a decimal point in it. However, commas are neither accepted in input nor produced in output. This applies even though e.g. {{NUMBEROFARTICLES}} uses comma delimiters every three digits, and in some localized versions of MediaWiki point delimiters.

Scientific notation is produced for numbers less than 1E-4 and for numbers greater than or equal to 1E+12 (see Template talk:Pow), but not accepted as input.

Thus:

  • {{#expr: 1/10000 *2}} gives 0.0002
  • {{#expr: {{#expr: 1/10000}} *2}} gives 0.0002.
  • {{#expr: 1/100000 *2}} gives 2.0E-5

but

  • {{#expr: {{#expr: 1/100000}} *2}} gives 2.0E-5.

See also