Difference between revisions of "Template:Convert"

From Wikishire
Jump to: navigation, search
m
m
 
(9 intermediate revisions by one other user not shown)
Line 1: Line 1:
<!-- ==Analyse elements==
+
<includeonly><!-- ==Analyse elements=
-->{{#vardefine:fig 1|{{uncomma|{{{1}}} }} }}
+
-->{{#vardefine:fig 1|{{uncomma|{{{1}}} }} }}{{#vardefine:plur 1|{{#ifexpr:{{#var:fig 1}}>1|y|n}} }}{{#vardefine:roundx|1}}<!--
{{#vardefine:plur 1|{{#ifexpr:{{#var:fig 1}}>1}}|y|n}}
+
=x-code (no metric)=
{{#vardefine:roundx|0}}<!--
+
-->{{#vardefine:x code|}}{{conv x-code|{{{3}}} }}{{conv x-code|{{{4}}} }}{{conv x-code|{{{5}}} }}{{conv x-code|{{{6}}} }}<!--
==x-code (no metric)==
+
=roundx code (rounding)=
-->{{#vardefine:x code|}}
+
-->{{#vardefine:roundx|{{conv Roundx def|{{{3}}}|{{{4}}}|{{{5}}}|{{{6}}}}}}}<!--
{{conv x-code|{{{3}}} }}{{conv x-code|{{{4}}} }}{{conv x-code|{{{5}}} }}{{conv x-code|{{{6}}} }}<!--
+
=Define units (unit 1, unit 1A, unit 2)=
==roundx code (rounding)==
+
-->{{#vardefine:unit 1|{{unitfix|{{{2}}} }} }}{{#vardefine:unit 1A|{{conv unit set-up|{{#var:unit 1}}|{{#var:plur 1}}|{{#switch:{{{abbr|}}}
-->{{#vardefine:roundx|{{#expr:{{#iferror:{{#expr:0+{{{3}}} }} }}+{{#iferror:{{#expr:0+{{{4}}} }} }}+{{#iferror:{{#expr:0+{{{5}}} }} }}+{{#iferror:{{#expr:0+{{{6}}} }} }} }} }}<!--
+
==Define units (unit 1, unit 1A, unit 2)==
+
-->{{#vardefine:unit 1|{{unitfix|{{{2}}} }} }}
+
{{#vardefine:unit 1A|{{conv unit set-up|{{#var:unit 1}}|{{#var:plur 1}}|{{#switch:{{{abbr|}}}
+
 
|on
 
|on
 
|in=abb
 
|in=abb
 
|out
 
|out
 
|off=long
 
|off=long
|def}}|{{{adj}}} }} }}
+
|def}}|{{{adj}}} }} }}{{#vardefine:unit 2|{{#if:{{SI|{{{3|}}} }}|{{unitfix|{{{3}}} }}|{{Data Units|{{#var:unit 1}}|counterpart}} }} }}<!--
{{#vardefine:tag 3|0}}
+
=Calculation=
{{#ifeq:{{{3}}}|x|{{#vardefine:tag 3|1}} }}
+
-->{{#vardefine:fig 2|{{convert 2|{{#var:fig 1}}|{{#var:unit 1}}|{{#var:unit 2}}|{{#var:roundx}} }} }}{{#vardefine:plur 2|{{#ifexpr:{{#var:fig 2}}>1|y|n}}}}<!--
{{#ifeq:{{#expr:{{{3}}}/{{{3}}} }}|1|{{#vardefine:tag 3|1}} }}
+
=Define unit 2A=
{{#ifeq:{{#var:tag 3}}|1|{{#vardefine:unit 2|{{Database|{{#var:unit 1}}|counterpart}} }}|{{#vardefine:unit 2|{{unitfix|{{{3}}} }} }} }}
+
-->{{#vardefine:unit 2A|{{conv unit set-up|{{#var:unit 2}}|{{#var:plur 2}}|{{#switch:{{{abbr|}}}
{{#vardefine:unit 2A|{{conv unit set-up|{{#var:unit 2}}|{{#var:plur 2}}|{{#switch:{{{abbr}}}
+
 
|on
 
|on
 
|out=abb
 
|out=abb
Line 26: Line 21:
 
|off=long
 
|off=long
 
|def}}|{{{adj|}}} }} }}<!--
 
|def}}|{{{adj|}}} }} }}<!--
=Calculation=
 
-->{{#vardefine:fig 2|{{convert 2|{{#var:fig 1}}|{{#var:unit 2}}|{{convert temp|{{#var:fig 1}}|{{#var:unit 1}}|{{#var:unit 2}}|{{#var|roundx}} }} }} }}
 
{{#vardefine:plur 2|{{#expr:{{#var:fig 2}}>1}}|y|n}}<!--
 
=Define unit 2A=
 
-->{{#vardefine:unit 2A|{{conv unit set-up|{{#var:unit 2}}|{{#var:plur 2}}|{{#switch:{{{abbr|}}}<!--
 
 
=Output display=
 
=Output display=
-->{{#ifeq:{{Database|{{#var:unit 1}}|quantity}}|{{Database|{{#var:unit 2}}|quantity}}|{{Conv display|{{#var:fig 1A}}|{{#var:unit 1}}|{{#var:fig 2A}}|{{#var:unit 2}}|{{#var:x code}} }}|'''''Error: mismatched units'''''}}
+
-->{{#ifeq:{{Data Units|{{#var:unit 1}}|quantity}}|{{Data Units|{{#var:unit 2}}|quantity}}|{{Conv display|{{#var:fig 1}}|{{#var:unit 1A}}|{{#var:fig 2}}|{{#var:unit 2A}}|{{#var:x code}} }}|'''''Error: mismatched units'''''}}</includeonly><noinclude>{{Convert/doc}}
<noinclude>'''THIS DOES NOT WORK YET, BUT GETTING THERE'''{{Documentation}}
+
 
+
[[Category:Convert templates|Convert]]
+

Latest revision as of 21:52, 8 February 2014


The Convert template converts between units of measurement. The basic format is:

{{convert | [value] | [unit] | [unit to convert to] | [how many decimal places] }}

The third and fourth elements (the "output unit" and the rounding figure) may be omitted, in which case the template will choose from the database a normal counterpart for the input unit and will assume all is rounded to 1 decimal place.

Thus {{convert|1,000|ft}}
will produce: 1,000.0 feet (304.8 m)

If one of the units is imperial, the programme will always put that one first.

Units

The system accepts a wide range of units and while there are codes for them, the template is worked so that any reasonable and unambiguous rendering of the name of the unit, if we have anticipated it, will be accepted (so a metre is "m", but entering "m", "metre" or "metres" for it will work too).

Additional, optional codes

There are two short codes, for rounding and the "x" code, which may be added after the unit or units. It does not matter which way round these go, as long as they are in a distinct field ("|3" or "|x").

Rounding

To chose the number of decimal places to which the answer should be rounded, add the number after the unit(s).

Imperial only: x code

If the non-imperial measurement is to be omitted (for example if the original material has metric only but you want to show an imperial unit in its place), add "x" after the unit(s):

Putting {{convert|300|m|x}} {{convert|300|m|3}}
will produce: 984.3 feet 984.252 feet (300.000 m)
Likewise {{convert|300|m|x|2}} or {{convert|300|m|2|x}}
will produce: 984.25 feet

Missing units

If there are useful units which do not work in the template, it is only a question of updating the database: notify the Wikishire folk on the Talk page and someone will no doubt sort it out.