Template talk:0expr

From MattWiki
Jump to: navigation, search

Documentation

{{0expr}} adds a leading zero to non-negative results less than 10 for date calculations.

Usage

{{0expr|expression}} - returns result as is, or 0result for 0 ≤ result < 10.
{{0expr|1=expression}} - ditto for expressions containing equal signs.
Updates:
  1. The code was simplified for better support of optional substitution (= no more parameter defaults), hence empty expressions are not more supported.
  2. Decimal fractions are now kept as is (1.25 results in 01.25), this can be useful for the seconds in timestamps hhmmss.nnn, see ISO 8601.

Example

Code Result
{{0expr|-1}} -1
{{0expr|123/100}} 01.2299999999
{{0expr|1+9}} 10
{{0expr|not 10}} 00
{{0expr|1=(7<=8)*9}} 09
{{0expr|(7<=8)*9|2+3}} 05

The last example shows that expressions containing "=", "<=", or ">=" won't work directly.

See also

Discussion

Add issues below as you see fit, sign with ~~~~

Improved code

I've written an updated code that will handle being passed no parameters (or a blank parameter). Output in such a case will be blank. The improvement is necessary for a template I'm working on and improves the stability of the current template.

{{#if:{{{1|}}}|{{{{{subst|}}}#ifexpr: 0 <= ({{#expr:{{{1}}}+0}}) and ({{#expr:{{{1}}}+0}}) < 10|0}}{{{{{subst|}}}#expr:{{{1}}}}}}}

New examples:

Code Result
{{0expr|}}
{{0expr}} expressionerror: Unrecognized punctuation character "{".

The current result for such cases is an error: update since

Code Result
{{0expr|}}
{{0expr}} expressionerror: Unrecognized punctuation character "{".


Previous test cases still work as expected:

Code Result
{{0expr|-1}} -1
{{0expr|123/100}} 01.2299999999
{{0expr|1+9}} 10
{{0expr|not 10}} 00
{{0expr|1=(7<=8)*9}} 09
{{0expr|(7<=8)*9|2+3}} 05

--rannpháirtí anaithnid (talk) 12:28, 25 May 2009 (UTC)

p.s. I'd do this myself only I have not been autoconfirmed yet. --rannṗáirtí anaiṫnid (coṁrá) 23:22, 26 May 2009 (UTC)

Yes check.svg Done --rannṗáirtí anaiṫnid (coṁrá) 12:15, 28 May 2009 (UTC)

padleft

Why does this not use the built-in padleft function? --- RockMFR 21:45, 25 July 2009 (UTC)

The original creator probably didn't know about that function. I didn't when I updated the code (above). Looks like a candidate to be simplified. --rannṗáirtí anaiṫnid (coṁrá) 00:57, 26 July 2009 (UTC)
Oops. I didn't realize this also supports non-integers. Nevermind. --- RockMFR 01:39, 26 July 2009 (UTC)

What is the subst parameter for?

This template has an undocumented "subst" parameter which doesn't seem to do anything, and is causing the syntax highlighter gadget to choke when viewing the source of this template. What is it for? If it's for removing all the #if and #expr statements when substituting, why not just use <includeonly>subst:</includeonly>? —Remember the dot (talk) 23:03, 1 January 2013 (UTC)

I've removed the subst parameter. Sorry for the first failed attempt. Let me know if there's something else I'm not seeing. —Remember the dot (talk) 06:00, 4 January 2013 (UTC)