Template:Convert

From WikiAlpha
Jump to: navigation, search
[edit] [purge] Template documentation
The below content is licensed according to Creative Commons Attribution-ShareAlike License contrary to the public domain logo at the foot of the page. It originally appeared on http://en.wikipedia.org. The original article might still be accessible here. You may be able to find a list of the article's previous contributors on the talk page.

Template:Transwiki guide

Template {{convert}} calculates measurements from one unit (you can enter) to another one, and then presents the results. For example:

{{convert|2|km|mi}} → 2 kilometres (1.2 mi)
{{convert|7|mi|km}} → 7 miles (11 km)

The available units are listed below. Units are always case-sensitive (Template:Xt, not Template:!xt or Template:!xt). Multiple formatting and calculation options are listed below.

Contents

Unit name or symbol (abbreviation): 1 pound or 1 lb?

By default, the first value shows unit name spelled out, the second the symbol (or abbreviation). Use |abbr=on/off to change this behaviour:

  • {{convert|1|lb|kg}} → 1 pound (0.45 kg)

To abbreviate both:

|abbr=in is the reverse behaviour to the default, |abbr=out:

Convenience: {{cvt}} has |abbr=on by default

Template {{cvt}} is similar to {{convert}}, but has |abbr=on as the default behaviour. In {{cvt}}, all other options are available. So:

{{cvt|1|lb|kg}}Template:Cvt

is equal to:

{{convert|1|lb|kg|abbr=on}}Template:Cvt

Adjective: a 10-mile distance

Use |adj=on to produce the adjective form:

  • A {{convert|10|mi|km|adj=on}} distance → A 10-mile (16 km) distance.

Default behaviour, for comparison:

  • {{convert|10|mi|km}} to go → 10 miles (16 km) to go.

This option does not work with |abbr=on.

Rounding: 100 ft is 30 m or 30.5 m or 30.48 m?

By definition, 100 ft equals Template:Convert/LoffAoffDoutSoff. In practical use, it is common to round the calculated metric number. With that, there are several possibilities.

Default rounding

By {{Convert}} default, the conversion result 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 rounding temperatures (see below).

Examples of rounding
Input Displays as Note
{{convert|123|ft|m|-1}} 123 feet (40 m)
{{convert|123|ft|m|0}} 123 feet (37 m)
{{convert|123|ft|m|1}} 123 feet (37.5 m)
{{convert|550|ft|m|0}} 550 feet (168 m) Approximate value is 167.64 m
{{convert|550|ft|m}} 550 feet (170 m) Rounds to 170, when approximate value is 167.64 m
{{convert|500|ft|m|0}} 500 feet (152 m) Approximate value is 152.4 m
{{convert|500|ft|m}} 500 feet (150 m) Rounds to 150, when approximate value is 152.4 m

Convert supports four types of rounding:

Round to a given precision: use a precision number

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 multiple of Template:Frac to the power of 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.

Round to a given number of significant figures: |sigfig=

To specify the output number to be with n significant figures use |sigfig=<number>:

Default behaviour, for comparison:

  • {{convert|1200|ft|m}} → 1,200 feet (370 m)

Setting |sigfig= is meaningless

Round to a multiple of 5: 15, 20, 25, ...

Using |round=5 rounds the outcome to a multiple of 5.

  • {{convert|10|m|ft}} → 10 metres (33 ft)
  • {{convert|10|m|ft|round=5}} → 10 metres (33 ft)

Similar: using |round=25 rounds the outcome to a multiple of 25.

  • {{convert|10|m|ft}} → 10 metres (33 ft)
  • {{convert|10|m|ft|round=25}} → 10 metres (33 ft)

Default behaviour, for comparison:

In a range, one can round each value individually to the default. Use |round=each:

  • {{convert|10 x 200 x 3000|m|ft}}Expression error: Unrecognized word "x". metres (Expression error: Unrecognized word "x". ft)
  • {{convert|10 x 200 x 3000|m|ft|round=each}}Expression error: Unrecognized word "x". metres (Expression error: Unrecognized word "x". ft)

Round to a multiple of a given fraction: Template:Frac inch

Specify the desired denominator using |frac=<some positive integer>. (Denominator is the below-the-slash number, for example the 3 in Template:Frac).

  • {{convert|5.56|cm|in|frac=16}} → 5.56 centimetres (2.19 in)
  • {{convert|8|cm|in|frac=4}} → 8 centimetres (3.1 in)

The fraction is reduced when possible:

  • {{convert|8|cm|in|frac=100}} → 8 centimetres (3.1 in)

Default behaviour, for comparison:

In temperatures: rounding °C, °F and K

In temperatures, 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.

  1. {{#invoke:DemoTemplate|convert|10,000|C|F K}}
  2. {{#invoke:DemoTemplate|convert|10,000.1|C|F K}}
  3. {{#invoke:DemoTemplate|convert|-272|C|F K}}
  4. {{#invoke:DemoTemplate|convert|-272|C}}
  5. {{#invoke:DemoTemplate|convert|-300|C|F K}}
  6. {{#invoke:DemoTemplate|convert|0|C|F K}}

The precision of the input number in example (1) is one digit, but the precision of its Kelvins expression is three, so the precision of the Fahrenheit conversion is made three (made 180...) . (1) and (2) seem to belie the fact that a Template:Convert/C-change, and make the 32 degrees difference shown in (1) begin to seem off somehow. Result (1) seems off until you set the significant figures yourself with |sigfig=:

{{#invoke:DemoTemplate|convert|10000|C|sigfig=5}}

or you set the precision positionally, relative to the decimal point (zero being at the decimal point):

{{#invoke:DemoTemplate|convert|10000|C|0}}

The precision of the input number in example (2) is six, so the precision of the Fahrenheit output is, whereas before, Kelvins had determined it to be three. Examples (3) and (4) show how this can be hidden and generate questions, but it occurs there because the Kelvins conversion generated two fractional parts. (Before it was the input number that generated the fractional part.) In example (3) the three input digits converted into into five significant output digits because of the two numbers after the decimal point, generated by the Kelvins conversion. This happened again in (5), but in (6) decimal fractions were neither given as input nor induced by the Kelvins conversion.

Into multiple units: Template:Convert/C

Separate the multiple output units by a space:

If the output unit names contain spaces, use + as the separator.

See also:

Ranges of values

For a conversion of a difference, see #Units of difference. A range converts two values and separates them by your choice of words and punctuation.

A range: 6 to 17 kg (13 to 37 lb)

Range indicators are entered as the second parameter (between the values). Range separators can be:
Template:Convert/doc/range separator list

Multiple dimensions: Template:Convert/x

Use by:

Use ×, multiplication sign, or x, letter:

In science, the formal way is to set |x| and |abbr=on (keeping dimensions right, like in area = x km2):

About feet, inch in ranges and multiples

While it is possible to enter feet, inch in a simple conversion, this is not possible for ranges:

Default behaviour, for comparison:

Words

Spelling of unit name: UK metre or US meter?

Default spelling of units is en-UK. To show en-US spelling, use |sp=us:

{{convert|1|m|ft}} → 1 metre (3.3 ft)—default
{{convert|1|m|ft|sp=us}} → 1 meter (3.3 ft)

Spell out numbers: ten miles

To write a number in words, use |spell=in:

  • {{convert|10|mi|m|spell=in}} → 10 miles (16,000 m)

To spell out both in and out values, use |spell=on:

  • {{convert|10|mi|m|spell=on}} → 10 miles (16,000 m)

To make first letter a capital, use |spell=In, |spell=On

  • {{convert|10|mi|m|spell=In}} → 10 miles (16,000 m)
  • {{convert|10|mi|m|spell=On}} → 10 miles (16,000 m)

Remember that the spelling of the units (ft, m) is independently set by |abbr=. To the extreme:

Inserted before units: 4 planted acres

disp=preunit is similar, but has no separator after the specified text, and can have different text for the output value:

After adjective unit: A Template:Convert/LoffAoffDoutSmid corridor

{{convert|10|ft|m|adj=mid|-long}}Template:Convert/LoffAoffDbSmid

Plurals: 1 inch, 2 inches

The unit symbol is singular always. Depending on the preceding number only, a unit name can be shown plural.

Exception

Entering the unit spelled |foot| forces singular output "foot", whatever the number is.

Fractions: one-eighth of an imperial pint

The convert template also supports spelling out fractions.

Any additional words needed for the fraction can also be added at the end of the template.

Numbers

Using an SI prefix: gigameter (Gm), or micrometer (μm)

Template:Sidebar metric prefixes (small) Units can have an SI prefix like G before the unit: Gm, and giga before the name: gigameter. These are plain multiplication factors.

To illustrate, these are trivial calculations (from meter to meter), showing the multiplication factor:

The prefix can be added before the SI unit (here: unit m for meter):

The prefix can be used in the output unit:

As an exception, the non-SI unit "inch" can have the "μ" prefix too)

Template:Further information

Engineering notation: 7 × 106 m

In the unit: e6m

Engineering number notations like 7Template:Smallcaps6 (for 7 × 106) can be entered as a "prefix" to the unit:

The same is possible for the output unit:

Any standard unit (not a combination, multiple, or built-in unit) can have such a prefix:

  • e3 (thousand),
  • e6 (million),
  • e9 (billion),
  • e12 (trillion),
  • e15 (quadrillion).

Scientific notation: 1.23×10−14

In scientific notation, a number is written like Template:Rnd/e−. The plain number has exactly one digit before the decimal point.

With {{convert}}, the input can be in e-notation such as 12.3e4. This value is displayed as a power of ten, and the output is displayed in scientific notation, except that an output value satisfying 0.01 <= v < 1000 is shown as a normal number. In addition, if the output value is 1000 and sigfig=4 is used, the value is displayed as a normal number.

Input with fractions: Template:Hands/numdisp/frac inches (38.1 mm)

The number to convert can be written in fractions. Both / (keyboard slash) and (fraction slash) are accepted:

  • {{convert|1/2|in|mm|1}}Template:Hands/numdisp/frac inches (12.7 mm)
  • {{convert|1⁄2|in|mm|1}}Expression error: Unrecognized punctuation character "�". inches (Expression error: Unrecognized punctuation character "�". mm)

With integers, use a + sign

  • {{convert|2+1⁄2|in|mm|1}}Expression error: Unrecognized punctuation character "�". inches (Expression error: Unrecognized punctuation character "�". mm)

When negative, use a hyphen - and repeat it:

  • {{convert|-2-1⁄2|in|mm|1}}Expression error: Unrecognized punctuation character "�". inches (Expression error: Unrecognized punctuation character "�". mm)
  • {{convert|2-1⁄2|in|mm|1}}Expression error: Unrecognized punctuation character "�". inches (Expression error: Unrecognized punctuation character "�". mm) Note: this is read as a range
  • {{convert|-2+1⁄2|in|mm|1}}Expression error: Unrecognized punctuation character "�". inches (Expression error: Unrecognized punctuation character "�". mm) Template:Nay Should be a number, not an expression (do not require a calculation)

Output with horizontal fraction bar in: Template:Sfrac inch

Using a double slash (//) returns a horizontal bar fraction:

  • {{convert|1//2|in|mm|1}}Expression error: Unexpected / operator. inches (Expression error: Unexpected / operator. mm)
  • {{convert|2+1//2|in|mm|1}}Expression error: Unexpected / operator. inches (Expression error: Unexpected / operator. mm)

Thousands separator: 1,000 mi or 1000 mi

In input, a comma for thousands separator is accepted but not required; a gap (space) is not accepted. In output, by default, the thousand separator is the comma:

  • {{convert|1234567|m|ft}} → 1,234,567 metres (4,050,420 ft)
  • {{convert|1,234,567|m|ft}} → 1,234,567 metres (4,050,420 ft)
  • {{convert|1 234 567|m|ft}}Expression error: Unexpected number. metres (Expression error: Unexpected number. ft) Template:Nay

Set |comma=off to remove the separator from the output:

  • {{convert|1234567|m|ft|comma=off}} → 1,234,567 metres (4,050,420 ft)

Use |comma=gaps to use digit grouping by gap (thin space) as a thousands separator:

  • {{convert|1234567|m|ft|comma=gaps}} → 1,234,567 metres (4,050,420 ft)

Default behaviour, for comparison:

  • {{convert|1234567|m|ft}} → 1,234,567 metres (4,050,420 ft)

Setting |comma=5 or |comma=gaps5 will only add the separator when the number of digits is 5 or more:

  • {{convert|1234|m|ft|comma=5}} → 1,234 metres (4,050 ft)
  • {{convert|1234567|m|ft|comma=5}} → 1,234,567 metres (4,050,420 ft)
  • {{convert|1234|m|ft|comma=gaps5}} → 1,234 metres (4,050 ft)
  • {{convert|1234567|m|ft|comma=gaps5}} → 1,234,567 metres (4,050,420 ft)

Default behaviour, for comparison:

  • {{convert|1234|m|ft}} → 1,234 metres (4,050 ft)

Output manipulation

Brackets and separators: 10 m [33 ft]

Punctuation that distinguishes the two measurements is set by |disp=.
Options are: b (the default), sqbr, comma, or, br, x|…:

Default behaviour, for comparison:

  • {{convert|10|m|ft}} → 10 metres (33 ft)

Setting |disp=br will force a new line (<br/>)

Also |disp=br() will force a new line, and keep the brackets:

Setting |disp=x|… allows any text as separator:

Flipping (reordering) the two measurements: Template:Convert/LoffAnoneDbSoff

Setting |order=flip will flip (swap) the two measurements:

Default behaviour, for comparison:

When converting to multiple units, the effect is:

Displaying parts of the result: Template:Convert/cuyd

It is possible to display only parts of the conversion result:

Convert Output Description
{{convert|2|cuyd|m3}} Template:Convert/cuyd Default behaviour, for comparison
{{convert|2|cuyd|m3|abbr=values}} Template:Convert/cuyd Input and output numbers
{{convert|2|cuyd|m3|disp=unit}} Template:Convert/cuyd Input unit
{{convert|2|cuyd|m3|disp=unit|adj=on}} Template:Convert/cuyd Input unit, adjective (hyphenated)
{{convert|2|cuyd|cuyd|0|disp=out|abbr=off}} Template:Convert/cuyd Input (workaround)
{{convert|2|cuyd|m3|abbr=~}} Template:Convert/cuyd Input: both name and symbol
{{convert|2|cuyd|m3|disp=unit2}} Template:Convert/cuyd Output unit (symbol)
{{convert|2|cuyd|m3|disp=unit2|abbr=off}} Template:Convert/cuyd Output unit (name)
{{convert|2|cuyd|m3|disp=number}} Template:Convert/cuyd Output value
{{convert|2|cuyd|m3|disp=out}} Template:Convert/cuyd Output value and unit
{{convert|2|cuyd|m3|disp=out|abbr=off}} Template:Convert/cuyd Output value and unit

Display both input name and symbol: 2 kilopascals [kPa]

Setting |abbr=~ returns both name and symbol of the first (input) unit:

Table options

For the wikitable structure, there are three options: add a line-break, split the result over columns and make the table sortable.

Enforced line break

|disp=br adds a line-break and omits brackets.

|disp=br() adds a line-break and does add brackets to the converted value. This may be useful in tables:

|disp=br |disp=br()
Template:Convert/LoffAoffDbrSoffTemplate:Convert/test/Dbr Template:Convert/LoffAoffDbr()Soff

Table columns showing numbers only

Using {convert} in a table cell, with |disp=table splits the result over two columns:

{{convert|10|m|ft|disp=table}}Template:Convert/LoffAoffDtableSoff

|disp=tablecen does the same, and also centers the text:

{{convert|20|m|ft|disp=tablecen}}Template:Convert/LoffAoffDtablecenSoff

The units are added as a column header:

  m ft
|disp=table Template:Convert/LoffAoffDtableSoff
|disp=tablecen Template:Convert/LoffAoffDtablecenSoff
|disp=<other> (default) Template:Convert/LoffAoffDSoff

Sorting

Use |sortable=on to include a hidden numerical sortkey in the output, suitable for use in a table with sortable columns. Technically, this places a hidden string before the actual displayed values:

{{convert|10|m|ft|sortable=on}} → <span style="display:none">7001100000000000000</span>10 metres (33&nbsp;ft)

Use both |disp=table and |sortable=on together to produce table columns (pipe symbols) for each value in sortable columns:

m ft
A Template:NtshTemplate:Convert/LoffAoffDtableSoff
B Template:NtshTemplate:Convert/LoffAoffDtableSoff
C Template:NtshTemplate:Convert/LoffAoffDtableSoff
D Template:NtshTemplate:Convert/LoffAoffDtableSoff

The generated sortkey is calculated in a consistent way based on both the value and its unit as passed to the convert template. In most cases convert uses the passed value converted to SI base units. It is therefore not necessarily the displayed value or other alternate units and is calculated regardless of output format options. Using different units or different order of units in individual rows should therefore not lead to incorrect sorting, although variations in rounding can give surprising results, since an unrounded number is used for the sortkey.

Units

All units

Template:Convert/list of units

'per' units: kg/hl

When using a slash (/), a unit like kg/hl is recognized as kilograms per hectolitre and will be converted with other mass/volume units.

Units of difference: 10 °C higher; how much in °F

When a number is the measurement for a difference, one conversion is done. This may occur in temperatures (°C, °F, K). Compare this to a range, when two conversions are done.

The only available units of difference are: |C-change=, |F-change= and |K-change=.

  • {{#invoke:DemoTemplate|convert|10|C}}, regular temperature
  • {{#invoke:DemoTemplate|convert|10|C-change}}, difference (e.g., "The temperature changed from 30 into 40 °C")
  • {{#invoke:DemoTemplate|convert|10-15|C}}, range (e.g., "The temperature is between 10 and 15 °C")

Consistently, into multiple units:

Multiple units: 1 ft 5 in

In input

Base document Template:Slink lists options for multiple unit input (like ft,in). It can catch predefined sets only (units that can be subdivided; e.g., yd into ft):

In output

Available multiple-unit output options predefined, like ftin and ydftin. The full list is at Template:Slink.

Default behaviour, for comparison:

See also:

  • {{hands}} a length used to measure horses
  • {{Long ton}} a weight in ton, cwt, qr and lb

Currency per unit: $/mi, €/km

Using currency symbols in a $ per unit value, you can convert the per-unit:

You can set the currency in both values using |$=€:

It is not possible to convert the currency. So, this result (mixed currencies) is not possible: Template:!mxt Template:Nay

Parameter list

Template:Convert/doc/parameter list

Deprecated options

Template:Convert/doc/deprecations list

TemplateData

This is the TemplateData documentation for this template used by VisualEditor and other tools.

See the monthly error report for this template.

TemplateData for Convert

<templatedata>{ "description": "Converts measurements to other units.", "params": { "1": {

 "label": "value",
 "description": "the value to convert",
 "type": "number"

}, "2": {

 "label": "from unit",
 "description": "",
 "type": "string/line"

}, "3": {

 "label": "to units",
 "description": "the unit to convert into",
 "type": "string/line"

}, "4": {

 "label": "precision or suffix",
 "description": "significant digits after decimal dot or, if negative, exponent of ten",
 "type": "number"

}, "lk": {

 "label": "link units",
 "description": "“on” all, “in” input, “out” output or “off” no units",
 "default": "off",
 "type": "string/line"

}, "abbr": {

 "label": "abbreviation",
 "description": "display for the units: “on” unit symbols, “off” all unit names in full words, “in” input unit symbol, “out” abbreviated output units, “values” no units at all",
 "default": "out",
 "type": "string/line"

}, "sp": {

 "label": "spelling",
 "description": "“us” display U.S. spelling of unit names",
 "type": "string/line"

}, "adj": {

 "label": "adjective",
 "description": "adjective form (singular unit name appended by hyphen) “on” or “mid” to put conversion at end",
 "type": "string/line"

}, "disp": {

 "label": "conversion",
 "description": "display conversion result: “or” after ‘or’, “x” with custom prefix and suffix, “b” in parentheses, “table”/“tablecen”, “output only” alone, “output number only” alone and without unit, “unit” not at all but input unit; if the value is a number it is used as precision",
 "default": "b",
 "type": "string/line"

}, "order": {

 "label": "ordering",
 "description": "“flip” returns converted value first, input value second.",
 "type": "string/line"

}, "sigfig": {

 "label": "significant figures",
 "description": "number that sets the number of significant figures",
 "type": "number"

}, "round": {

 "label": "rounding output",
 "description": "“5” rounds the output number to nearest multiple of 5, “25” to nearest multiple of 25, “each” rounds each number in a range",
 "type": "string/line"

}, "comma": {

 "label": "thousands separator",
 "description": "Sets or suppresses thousand separator in the numbers. “off” = no separator; “gaps”: use space not comma; “5” and “gaps5”: only add separator when number > 5 positions (10,000 or more)",
 "default": "on",
 "type": "string/line"

}, "sortable": {

 "label": "sort key",
 "description": "“on” generates a hidden sort key",
 "type": "string/line"

} }}</templatedata>

See also

Template:Convert navs