Jump to content

Template:Calculator/doc

From Wikipedia, the free encyclopedia

This template is for creating interactive calculators. It requires the calculator gadget. You must enable it in your preferences or add ?withgadget=calculator to the url for this template to work.

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

Example

[edit]
{{calculator|id=a|default=2|size=4}} × {{calculator|id=b|default=2|size=4}} = {{calculator|id=c|formula=a*b|default=4|size=8|readonly=true|type=text}}

produces:

2 × 2 = 4

BMI calculator metric

[edit]
{| class="wikitable" style = "float: left; margin-left:15px;"
|+ Metric
|-
| Weight || {{calculator|id=weightkg|size=3}} kg
|-
| Height || {{calculator|id=heightcm|size=3}} cm
|-
| BMI || '''{{calculator|id=bmimetric|type=plain|formula=round(weightkg/pow(heightcm/100,2))|style=min-width:3ch;display:inline-block}} kg/m<sup>2</sup>'''
|}
Metric
Weight kg
Height cm
BMI kg/m2

BMI calculator imperial

[edit]

You can put widgets inside tables:

<table class="wikitable">
<tr><td>Imperial</td></tr>
<tr><td>Weight</td><td>{{calculator|id=weight|size=1}} lbs</td></tr>
<tr><td>Height</td><td>{{calculator|id=heightFeet|size=1}} feet {{calculator|id=heightInches|size=1}} inches</td></tr>
<tr><td>BMI</td><td>'''{{calculator|id=bmi|type=plain|formula=round(weight*703/pow(heightFeet*12+heightInches,2))}} kg/m<sup>2</sup>'''</td></tr>
</table>
Imperial
Weight lbs
Height feet inches
BMI kg/m2

Template arguments

[edit]

Note: Not all arguments work on all input types.

id
The id for this field, used in formulas of other fields (Has to be english characters)
default
The starting value
readonly
Make field read only
formula
The formula to calculate this field. See below for what is supported
type
Type of field. Currently supported are plain, number, text, radio and checkbox
size
how big to make the input box (In terms of number of letters that can fit in the box)
max
Max number allowed (number type only)
min
Min number allowed (number type only)
placeholder
Placeholder text that shows up light grey when there is no input
step
How big the interval is for type=number inputs
style
Custom CSS to use for the element.
name
When using type=radio, the name of the radio group.

Formula

[edit]

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', 'round', '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 following additional functions are supported which are not from javascript Math library:

  • ifzero - if first argument is epsilon away from zero, return second argument, otherwise third argument
  • ifnan - if first argument is not a number (NaN), return second argument, otherwise third argument
  • iffinite - if the first argument is finite return second argument, otherwise return third argument.
  • coalesce - return the first argument that is not NaN.
  • ifpositive - if first argument is ≥ 0 return second, otherwise return third.

Numbers can be integers, decimals or scientific notation. For example: 1, 2.84543, 3.12E6, -5

The following constants are supported: Infinity, -Infinity, NaN, pi, π

At the moment, relational operators like ≤ or ≠ and IF statements are not supported. This might be added in a future version if needed.

Add a calculator widget to the page. Like a spreadsheet you can refer to other widgets in the same page.

Template parameters

ParameterDescriptionTypeStatus
idid

The id for this input. This is used to reference it in formula of other calculator templates

Stringrequired
typetype

What type of input box

Suggested values
plain number text radio checkbox
Stringrequired
formulaformula

Formula to calculate this field

Example
3*log(a)
Stringsuggested
readonlyreadonly

Make input box readonly to user input

Booleanoptional
sizesize

Size of input box (How many characters it will fit)

Numberoptional
maxmax

max number allowed (type=number inputs only)

Numberoptional
minmin

min number allowed (type=number inputs only)

Numberoptional
placeholderplaceholder

Text to put as a placeholder in empty input

Stringoptional
stepstep

How much to increment a type=number input box

Example
2.5
Numberoptional
defaultdefault

Default value for this field

Stringsuggested
stylestyle

CSS to style the input element with

Stringoptional
namename

For type=radio what group to assign the radio button to

Unknownoptional