Template talk:Twodigit
Add topicPrefix single digit with zero
[edit]This template is used to implement Template:CURRENTDAY2 which displays the current month in two-digit form (i.e. months below 10 are prefixed with 0, e.g. 01, 02, 03, ..., 09, 10, 11, 12).
Template:Twodigit takes a single argument of any length. The resulting value will be zero-prefixed only if it is a single digit integer greater than 0.
Examples
[edit]What it looks like | What you type |
---|---|
01 | {{Twodigit|1}} |
10 | {{Twodigit|10}} |
0 | {{Twodigit|0}} |
999999999999 | {{Twodigit|999999999999}} |
27 | {{Twodigit|{{CURRENTDAY}}}} |
How It Works
[edit]The underlying source code of this template is:
{{Twodigit_a||||||||||{{{1}}}=0|val={{{1}}}}}
It works by calling Template:Twodigit_a with parameters 1-9 first set to the empty string. It also stores the value 0 in the parameter position referenced by its argument, and the value of this argument in the parameter named val.
For example if Template:Twodigit is called using {{Twodigit|9}} then it will call Template:Twodigit_a with parameters 1-8 set to the empty string, parameter 9 set to 0 and parameter val set to 9.
The code used in Template:Twodigit_a is:
{{{1}}}{{{2}}}{{{3}}}{{{4}}}{{{5}}}{{{6}}}{{{7}}}{{{8}}}{{{9}}}{{{val}}}
It works by simply concatenating parameters 1-9 followed by parameter val.
When Template:Twodigit is called with a numerical argument between 1 and 9 then Template:Twodigit_a will have a 0 in one of its parameters 1-9 which will have the effect of prefixing val with 0.
When Template:Twodigit is called with an numerical argument less than 1 or greater than 9 then Template:Twodigit_a's parameters 1-9 will be blank and val will be returned on its own.
Please note: I have made the mistake of referring to en.wikipedia.org's implementation of this as a function. It actually uses static templates for each possible value (e.g. Twodigit_1, Twodigit_2, Twodigit_10, etc) and therefore is limited to converting only those values where a template exists. In contrast, this template is capable of converting any value that is passed to it.
See also
[edit]MediaWiki version: | 1.6 | (for parameters defaults) |
- Template:Twodigit alt - different technique with the same result
- w:en:Template:CURRENTDAY2 - different technique, applied to {{CURRENTDAY}}, using 31 auxiliary templates [1]