Template:Convert
{
This template is used on 650,000 pages. To avoid large-scale disruption and unnecessary server load, any changes to this template should first be tested in the template's /sandbox or /testcases subpages, or in your own user space. The tested changes can then be added in one single edit to the template. Please consider discussing any changes on the talk page before implementing them. |
48px | See how to copy this template for information on copying this template and modifying it for use on another wiki. |
This template uses Module:Convert, a script written in the Lua programming language. Please see Wikipedia:Lua if you want to learn more about Lua or help convert other templates. |
Usage
- See Help:Convert for up-to-date information.
This template shows measurement quantities as primary and converted secondary units (or ranges), with optional rounding, wikilinks, abbreviation, or US spellings, and various separators including: parentheses "( )", "or", hyphens, comma, "to" or dashes. It can help users by calculating correct conversions from one unit of measurement to another, particularly for more complex units. It's also useful by making the text adhere to the Manual of Style.
Styles include: distance "Template loop detected: Template:Convert/mTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/" and "Template loop detected: Template:Convert/mTemplate:Convert/track/abbr/Template:Convert/track/disp/orTemplate:Convert/track/adj/"; temperature "Template:Convert/°CTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/"; mass "Template:Convert/kgTemplate:Convert/track/abbr/onTemplate:Convert/track/disp/Template:Convert/track/adj/" or "Template:Convert/toTemplate:Convert/track/abbr/onTemplate:Convert/track/disp/Template:Convert/track/adj/"; etc. For units see: Convert/list of units.
Options include: | lk=in , abbr=on , abbr=off , sp=us , adj=on , adj=mid , disp=x , disp=table and sortable=on (explained below). By default, output is rounded to match the precision of input; alternatives include: sigfig=3 or using the end parameter, such as "|1 " to show tenths or "|-2 " to show full 100's, etc.
|
Single value:
{{convert|value|in_unit|out_unit|round_to|...}}
Range of 2 values, or set of 3 or 4 (see below for list of range options):
{{convert|val1|range|val2 |in_unit|out_unit|round_to|...}} {{convert/3 |val1|range|val2|words|val3|in_unit|out_unit|round_to|...}} {{convert/4|val1|words|val2|words|val3|words|val4|in_unit|out_unit|...}}
- Optional parameters, in examples below, allow abbreviated units (abbr=on), or U.S. spellings (sp=us for meter), or hyphenation (adj=on), etc.
- This template may in some cases directly convert to other base units, for example
{{convert|29.8|km/s|mi/h}}
will directly convert the km/Template:Red to mpTemplate:Red, as in Template:Convert/km/sTemplate:Convert/track/abbr/onTemplate:Convert/track/disp/Template:Convert/track/adj/. - For this template many levels of substitution are possible, hence doing this fully, step by step, is cumbersome. Use Special:ExpandTemplates instead.
- The template provides some dimensional analysis to check for compatible units. The user should be careful that input and output units are compatible. Do not, for example, attempt to convert barrels to tonnes (but see {{bbl to t}}). Also beware of units with similar names; the code
oz
, for example, is for the avoirdupois ounce (a unit of mass), do not use this if you want fluid ounces. Note also that the codelb
gives pounds mass, for pounds force uselbf
. - This documentation page tends to lag behind in terms of what the template can do. For more details, see the talk page and its archives.
Examples
Single value
Range of 2 values
- Note: not yet operational for all units and it may be necessary to enter as "convert/2" rather than simply "convert"
The following conversions are currently not available as a range of values:
- Some imperial and US gallon-based units
- Other fuel-consumption units, except "mpg" or "L100km" shown above.
- Large scale units (e3, e6, e9)
- Combination-based units (ft&in, st&lb, lb&oz) due to complexity of the conversion.
Range/set of 3 or 4 values
- Note: not yet operational for all units
Input | Displays as |
{{convert |60|by|100|by|120|m|ft}} | Template:Convert/byTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/ |
{{convert/3 |60|x|100|x|120|m|ft|abbr=mos}} | Template:Convert/3 |
{{convert/3 |60|-|70|-|80|kg|lb}} | Template:Convert/3 |
{{convert/4 |60|-|70|-|80|-|90|kg|lb}} | Template:Convert/4 |
{{convert/3 |60|to|80|or|85|m|ft}} | Template:Convert/3 |
{{convert/3 |11|by|15|rarely|16|ft|m}} | Template:Convert/3 |
{{convert/3 |41|to|50|to|60|F|C}} | Template:Convert/3 |
{{convert/4 |2|to|12|, in summer|25|to|36|C|F}} | Template:Convert/4 |
{{convert/3 |41|-|50|almost|51|F|K}} | Template:Convert/3 |
For {{convert/3}} or {{convert/4}}, the separator words can be any text.
Parameters
Parameters | |
---|---|
Use US spelling | attach |sp=us Template:Nsto show "meter" rather than "metre" (etc.)
|
Make units linked | attach |lk=on to link all units (default: lk=off)attach |lk=in to link only the input unitsattach |lk=out to link only the output units(The manual of style suggests that you should not link common units of measurement). |
Use the adjective form, or insert mid-text words | attach |adj=on (e.g. "190-foot (58 m) bridge" instead of "190 feet...").
attach
Note 1: Any rounding-parameter should follow the mid-text: "text|0".
|
Abbreviate units, or not | attach |abbr=on Template:Nsto show unit symbolsattach |abbr=off to show all unit names in full wordsattach |abbr=in Template:Nsto abbreviate input unitsattach |abbr=out Template:Nsto abbreviate output unitsattach |abbr=~ Template:Nsto show input unit symbol in resultattach |abbr=values to suppress unit names & show only values. So, {{convert|6|mi|abbr=values}} gives: Template:Convert/miTemplate:Convert/track/abbr/valuesTemplate:Convert/track/disp/Template:Convert/track/adj/.
|
Change "( )" to display other separators | attach |disp=or to put "or" between units.The default value is: disp=b for brackets/parentheses. |
Change "( )" to customized separators | attach |disp=x| (begin | end) to show "xx (begin yy end)" See example 1attach |disp=x|; to show "xx; yy" See example 2attach |disp=x| (same as |) to show "xx (same as yy)". See example 3Example 1: {{convert|9|km|mi|disp=x| [|]}}→9 kilometres [5.6 mi] (note space before [|) Example 2: {{convert|9|km|mi|disp=x|; }}→9 kilometres; 5.6 mi Example 3: {{convert|10|km|mi|disp=x| (about |)}}→10 kilometres (about 6.2 mi) (note spaces used within the code). |
Display output only | attach |disp=output only to show result number & unitattach |disp=output number only to show just numberNote when using "disp=output only" then the unit name can still be shown as full words by abbr=off or linked by lk=on. Example: {{convert|7500|acre|m2|disp=output only }} → Template:Convert/acreTemplate:Convert/track/abbr/Template:Convert/track/disp/output onlyTemplate:Convert/track/adj/ |
Display units inverted as reverse order | attach |disp=flip to reverse the order & show output unit first.So, {{convert|6|km|disp=flip}} shows "3.7 miles (6 km)". To get symbol "mi" use abbr=in, to abbreviate the input unit (left-side unit). |
Display unit name (only) | attach |disp=unit to show the unit name for a symbolThe unit name will be plural when the amount is not 1, or hyphenated when adj=on. So, {{convert|2|cuyd|disp=unit}} shows "cubic yards" plural. |
Display amounts as table cells | attach |disp=table (or |disp=tablecen) Note: For use in tables, the template must start on a new line after a pipe. Only the number will be displayed unless you set |abbr=on , |lk=on , |lk=in or |lk=out . Example: here
|
Round to a specified number of significant figures | attach |sigfig={some integer greater than zero} . In the case of temperatures Celsius or Fahrenheit this refers to the difference with the absolute-zero temperature. For example, at room temperature two significant digits means rounded to tens of degrees.
|
Round by 5 | attach |disp=5 to round the output amount to the nearest 5 units. Not supported for all options. |
Let Convert decide the unit to convert to | skip the precision parameter (the 3rd or 4th unnamed parameter) e.g. {{convert|100|km|ft}} gives Template:Convert/kmTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/ and {{convert|100|km}} gives Template:Convert/kmTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/.
|
Display input value with a fraction | {{convert|3/8|in|mm|3|abbr=on}} → Template:Convert/inTemplate:Convert/track/abbr/onTemplate:Convert/track/disp/Template:Convert/track/adj/ or {{convert|11+1/4|in|cm|2|abbr=on}} → Template:Convert/inTemplate:Convert/track/abbr/onTemplate:Convert/track/disp/Template:Convert/track/adj/
For negative amounts, use two minus signs (hyphens): -11-1/4. |
Generate a hidden sort key for use with a sortable table | attach |sortable=on to generate a hidden sort key so that sortable tables will sort correctly. This uses {{ntsh}} to generate a sort key from the first numeric value. It ignores any additional values, ie, if you use 6|ft|2|in as the value, it will only use the 6 for the sort key. This will cause numeric values to sort in numeric order, ie: 5, 10, 15 instead of 10, 15, 5.
|
Parameters still under construction
Parameters still under construction. May not work in all situations | |
---|---|
disp=5[note 1] | The output will be rounded to the nearest 5 units. Might not work when linking unit names. |
disp=tablecen[note 1] | Similar to disp=table except that the values with be centered within the column. See the tables at La Nouvelle branch, BM-21 Grad for in use examples. |
abbr=in | Abbreviates only the input unit. |
abbr=out | Abbreviates only the output unit. |
disp=br | This separates input and output units with a forced line break. This can be useful in tables with limited horizontal space. |
disp=sqbr | Shows square brackets "[ ]" instead of parentheses "( )" in output. Example: 55 miles [89 km]. This option can be used in direct quotes, to show conversions in editorial brackets. See: attach |disp=x|[|] , above, as another way of showing brackets "[ ]".
|
Rounding
Convert supports four types of rounding:
- Rounding to a given precision
- Specify the desired precision with the fourth unnamed parameter (or third unnamed parameter if the "convert to" parameter is omitted; or fifth unnamed parameter if a range is specified; or fourth unnamed parameter again if a range is specified and the "convert to" parameter is omitted; needs to be replaced with a "precision" named parameter). The conversion is rounded off to the nearest power of Template:Frac this number. For instance, if the result is 8621 and the round number is '-2', the result will be 8600. If the result is '234.0283043' and the round number is '0', the result will be 234.
- Rounding to a given number of significant figures
- Specify the desired number of significant figures using
|sigfig={some integer greater than zero}
as noted above.
- Default rounding
- If neither the desired precision nor the desired number of significant figures is specified, the conversion will be rounded either to precision comparable to that of the input value (the number of digits after the decimal point—or the negative of the number of non-significant zeroes before the point—is increased by one if the conversion is a multiplication by a number between 0.02 and 0.2, remains the same if the factor is between 0.2 and 2, is decreased by 1 if it is between 2 and 20, and so on) or to two significant figures, whichever is more precise. An exception to this is temperature, wherein the conversion will be rounded either to precision comparable to that of the input value or to that which would give three significant figures when expressed in kelvins, whichever is more precise.
Examples of default rounding | ||
---|---|---|
Input | Displays as | Note |
{{convert|550|ft|m|0}} | Template:Convert/ftTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/ | Approximate value is 167.64 m |
{{convert|550|ft|m}} | Template:Convert/ftTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/ | Rounds to 170, when approximate value is 167.64 m |
{{convert|500|ft|m|0}} | Template:Convert/ftTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/ | Approximate value is 152.4 m |
{{convert|500|ft|m}} | Template:Convert/ftTemplate:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/ | Rounds to 150, when approximate value is 152.4 m |
Units supported
Examples
- Monitor quality or look at examples at {{Convert/check}}.
TemplateData
See the monthly error report for this template.
TemplateData for Convert
Converts measurements to other units.
Parameter | Description | Type | Status | |
---|---|---|---|---|
value | 1 | the value to convert | Number | optional |
from unit | 2 | Line | optional | |
to units | 3 | Line | optional | |
precision or suffix | 4 | significant digits after decimal dot or, if negative, exponent of ten | Number | optional |
link units | lk | “on” all, “in” input, “out” output or “off” no units
| Line | optional |
abbreviation | abbr | show “on” unit symbols, “off” all unit names in full words, “in” abbreviated input units, “out” abbreviated output units or “values” no unit names | Line | optional |
spelling | sp | “us” US | Line | optional |
adjective | adj | adjective form (singular unit name appended by hyphen) “on” or “mid” to put conversion at end | Line | optional |
conversion | disp | show conversion … “or” after ‘or’, “b” in parentheses, “x” with custom prefix and suffix, “table”/“tablecen”, “output only” alone, “output number only” alone and without unit, “flip” first, “unit” not at all but input unit; if the value is a number it is used as precision
| Line | optional |
significant figures | sigfig | number that sets the number of significant figures | Number | optional |
sort key | sortable | “on” generate a hidden sort key | Line | optional |
See also
- Bug 235: Auto unit conversion
- Category:Convert error categories
The above documentation is transcluded from Template:Convert/doc. (edit | history) Editors can experiment in this template's sandbox (create | mirror) and testcases (create) pages. Please add categories and interwikis to the /doc subpage. Subpages of this template. |
Template:Convert/track/abbr/Template:Convert/track/disp/Template:Convert/track/adj/