This template is for creating interactive calculators. It requires the calculator gadget. The calculator works by default. You can opt out at: Preferences > Gadgets > Template Gadgets > Enables javascript Calculator template.
You can use this template multiple times on a page to make input widgets, with some of the widgets having formulas based on other widgets, like a spreadsheet.
You can use {{Calculator label}} to mark text as a label for a calculator widget.
Statistics for this template are available at [1]
{{calculator|id=a|default=2|size=4}} × {{calculator|id=b|default=2|size=4}} = {{calculator|id=c|formula=a*b|default=4|type=plain}}
produces:
2 × 2 = 4
{{calculator|id=km|type=number|size=9|default=1.609344|formula=miles*1.609344}} km = {{calculator|id=miles|type=number|size=9|default=1|formula=km/1.609344}} miles
produces:
1.609344 km = 1 miles
You can put widgets inside a wikitable.
{| class="wikitable" style = "float: left; margin-left:15px;" |+ Metric |- | {{calculator label|Weight|for=weightkg}} || {{calculator|id=weightkg|size=3|default=80}} kg |- | {{calculator label|Height|for=heightcm}} || {{calculator|id=heightcm|size=3|default=160}} cm |- | BMI || '''{{calculator|id=bmimetric|type=plain|formula=round(weightkg/pow(heightcm/100,2))|default=31|style=min-width:3ch;display:inline-block}} kg/m<sup>2</sup>''' |}
Weight | 80 kg |
Height | 160 cm |
BMI | 31 kg/m2 |
You can also put widgets inside an HTML table.
<table class="wikitable"> <tr><td>Imperial</td></tr> <tr><td>Weight</td><td>{{calculator|id=weight|size=3}} lbs</td></tr> <tr><td>Height</td><td>{{calculator|id=heightFeet|size=1}} feet {{calculator|id=heightInches|size=2}} inches</td></tr> <tr><td>BMI</td><td>'''{{calculator|id=bmi|type=plain|formula=round(100*weight*703/pow(heightFeet*12+heightInches,2))/100}} kg/m<sup>2</sup>'''</td></tr> </table>
Imperial | |
Weight | lbs |
Height | feet inches |
BMI | kg/m2 |
When pressed, the {{calculator button}} updates a form field.
{{calculator button|contents=Click me!|for=buttoncount|formula=buttoncount+1|type=plain}} The button has been clicked {{calculator|type=plain|default=0|id=buttoncount}} times.
The button has been clicked 0 times.
A {{calculator label}}
{{calculator|type=checkbox|id=checklabelex}} {{calculator label|label=Please click this label|for=checklabelex}}
Please click this label
Formulas use normal math syntax, with english words representing other input boxes. e.g. sin(1+foo*2) would multiply the foo box by 2, add 1 and take the sine of the whole thing. All calculations are done using IEEE 754 double precision floating point numbers.
Operators supported include: +, -, *, ×, /, ÷, % (percent is the modulo operator). Exponentiation must use the pow() function.
Math functions supported include: 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'cos', 'cosh', 'exp', 'floor', 'hypot', 'log', 'log10', 'log2', 'max', 'min', 'pow', 'random', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'
These have the same definition as in javascript. In particular, that means that log() is log base-e. The exception is round, which we use our own definition of.
The following additional functions are supported which are not from javascript Math library:
index( foo, 2+3 )
is the same as foo5
. The first argument must be an identifier (Nested index() is not allowed). The second argument must evaluate to an integer greater than or equal to 0. If either of these two arguments are invalid, NaN is returned. The third argument is returned if the arguments are valid but the result references a variable that does not exist. If unspecified the third argument defaults to NaN.Numbers can be
At the moment, relational operators like ≤ or ≠ and IF statements are not supported. This might be added in a future version if needed. This can be worked around by using ifzero and ifpositive functions.
If the calculator widgets are contained within an element with the class calculator-container
, then the ids are scoped to this element. This allows you to use the same ids multiple times on the same page without them interfering with each other. If the scoping element has the attribute data-calculator-refresh-on-load="true"
, then the calculator widgets are refreshed on first page load before the user interacts with it.
For example:
<div class="calculator-container" data-calculator-refresh-on-load="true"> {{calculator label|scopedfield:}} {{calculator|type=text|id=scopedfield|default=3.14}}<br> This formula is updated before user interaction: {{calculator|type=plain|formula=scopedfield|default=No value}} </div> This formula is not updated because it is in a different scope: {{calculator|type=plain|formula=scopedfield|default=No value}}
Produces the following:
scopedfield: 3.14
This formula is updated before user interaction: No value
This formula is not updated even after user interaction because it is in a different scope: No value
Instead of using the css classes directly, you can also use the {{Calculator ifenabled}} with the scoped parameter.
You can use CSS to adjust the display, either via TemplateStyles or inline styles.
data-calculator-field-value
attribute. e.g. #calculator-field-fieldidhere[calculator-field-scopedfield^="3."] would select the field if it is >= 3.0 and < 4.0.Some examples:
Adjust color of border: 30
It is possible to make widgets be in MediaWiki (Codex) style instead of OS style. You can use most codex widgets that have a CSS-only version. See https://doc.wikimedia.org/codex/latest/components/demos/
Many codex style widgets have separate templates. When making a label, the {{Calculator label}} template has a parameter to output a codex style label
Template | Example |
---|---|
{{Calculator codex text}} | Label textShort description text
42
|
{{Calculator codex radio}} | This is a group of options
First optionAdditional text describing the first option Second optionAdditional text describing the first option Thid option |
{{Calculator codex checkbox}} | First optionAdditional text describing the first option |
{{Calculator codex toggle}} | My toggleAdditional text describing the toggle
|
{{Calculator button}} |
|
See the individual template doc pages for all the options.
Users who do not have the gadget enabled in their preferences or have JS disabled, will not see the input boxes. Instead they will just see the default value for each box. With a good choice of default, this can be sufficient.
For example, if you have ''sin({{calculator|id=sine|type=text|default=0.5|size=4}}π)={{calculator|type=plain|default=1.00|decimals=2|formula=sin(sine*π)|id=sineres}}'' to make sin(0.5π)=1.00, the non-js user will lose the interactivity, but they will still see the equation.
If you want more sophisticated fallbacks, you can use the calculatorgadget-enabled
and calculatorgadget-fallback
CSS classes.
<div class="calculatorgadget-enabled" style="display:none">This text is only shown if the gadget is enabled. {{calculator|type=text|size=20|default=100|id=xyz}} </div> <div class="calculatorgadget-fallback">This text is only shown if the gadget is disabled</div>
Which produces:
Alternatively, you can use the {{Calculator ifenabled}} template to do this.
checkbox | 1Wet | |
0 Big | ||
hidden | 160 Not visible, does not take space. Useful for intermediate results in formulas. | |
number | 80 | |
passthru | Is not visibly changed but the css class changes based on the formula User:Bawolff, I've set the formula to =wet, the checkbox above. Now what should happen? | |
plain | 2 | |
radio | 1 Metric | |
0 Imperial | ||
text | 160 | |
range | 160 160 |
Add a calculator widget to the page. Like a spreadsheet you can refer to other widgets in the same page.
Parameter | Description | Type | Status | |
---|---|---|---|---|
id | id | The id for this input. This is used to reference it in formula of other calculator templates | String | required |
type | type | What type of input box
| String | required |
formula | formula | Formula to calculate this field
| String | suggested |
readonly | readonly | Make input box readonly to user input | Boolean | optional |
size | size | Size of input box (How many characters it will fit) | Number | optional |
max | max | max number allowed (type=number inputs only) | Number | optional |
min | min | min number allowed (type=number inputs only) | Number | optional |
placeholder | placeholder | Text to put as a placeholder in empty input | String | optional |
step | step | How much to increment a type=number input box
| Number | optional |
default | default | Default value for this field | String | suggested |
style | style | CSS to style the input element with | String | optional |
name | name | For type=radio what group to assign the radio button to | Unknown | optional |
precision | precision | Format to this many significant digits, using decimal notation except for really large numbers [Only applies to type=number or type=plain]
| Number | optional |
exponential-precision | exponential-precision | Format to this many significant digits, using scientific notation [Only applies to type=number or type=plain]
| Number | optional |
decimals | decimals | Format to a fixed number of decimal digits [Only applies to type=number or type=plain]
| Number | optional |
NaN-text | NaN-text | Use this text instead of NaN to signify "not a number". Only applies to format=plain or format=text. Plaintext only; wikitext is not supported
| String | optional |
class | class | CSS classes to add | String | optional |
class-live | class-live | CSS class to add only if gadget is active on the page | String | optional |