Difference between revisions of "Template:Hands/numdisp"
Jump to navigation
Jump to search
imported>Plastikspork (This should work as well) |
imported>DePiep m (→top: in documentation: Protect -{ markup in preprocessor lang-conversion: phab:T146304, mw:explained using AWB) |
||
| (8 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
| − | + | <noinclude><!-- | |
| − | |||
| − | |||
| − | |||
| − | |||
--============================================================ | --============================================================ | ||
-- Display a number, with optional fraction part: 6+3/8 | -- Display a number, with optional fraction part: 6+3/8 | ||
--============================================================ | --============================================================ | ||
| + | -- (see NOTES at bottom) | ||
| + | -- | ||
| + | --></noinclude>{{#if:{{#titleparts:{{{1|-1200-3~118}}}|1|2}} | ||
| + | |{{hands/numdisp/frac | ||
| + | |{{{1|-1200-3/118}}}|{{#titleparts:{{{1|-1200-3/118}}}|1|1}}|{{#titleparts:{{{1|-1200-3/118}}}|1|2}}}}<!--split at "/" | ||
| + | --else-->|{{#ifexpr:{{{1|-14000.00}}}*0 = 0 and {{{1|-14000.00}}} < 0 | ||
| + | |<!--then re-build negative number (see NOTE Z4 below) | ||
| + | -->{{formatnum: | ||
| + | {{padright:−<!--&minus-->{{#expr:-( {{{1|-14000.00}}} ) }}<!--endexpr | ||
| + | --put decimal if needed-->{{ | ||
| + | #ifexpr:{{{1|-14000.00}}}0={{{1|-14000.00}}} | ||
| + | and {{{1|-14000.00}}}=floor( {{{1|-14000.00}}} )|.}}<!-- | ||
| + | padlength-->|{{#expr: | ||
| + | {{strlen_short|{{{1|-14000.00}}}|}}<!--endstrlen_short--> + {{ | ||
| + | #ifeq:&minus| {{padleft:|6|{{{1|x}}} }} |-6|0}} }}<!--endexpr | ||
| + | -->|0}}<!--endpadright end-zeroes "0" to original length | ||
| + | -->}}<!--endformatnum | ||
| + | else-->|{{formatnum: {{{1|6000.500}}} }}}}<!--endif {1}<0 | ||
| + | -->}}<noinclude><!--endif {#titleparts..} | ||
| + | -------------------------------------------------------------- | ||
| + | --NOTES: | ||
| + | -- [ These comments are skipped by <noinclude> and not sent | ||
| + | -- in formatted Internet pages, only during edit. ] | ||
-- | -- | ||
--NOTE A1: This template processes a number, for display, by | --NOTE A1: This template processes a number, for display, by | ||
| − | -- checking to display a fraction part ("/"), else it | + | -- checking to display a fraction part ("/"), else it |
| − | -- | + | -- will check for negative to display a unicode &minus |
| + | -- and {{formatnum:{{#expr:abs( {{{1|-6000}}} )}}}} such | ||
| + | -- as "-6000" with a full minus sign rather than hyphen. | ||
-- In 99% of Wikipedia usage, there are no fractions. | -- In 99% of Wikipedia usage, there are no fractions. | ||
-- | -- | ||
| Line 16: | Line 37: | ||
-- #titleparts to split the number at "/" (if any) and | -- #titleparts to split the number at "/" (if any) and | ||
-- then checks part 2 (the denominator, after the slash) | -- then checks part 2 (the denominator, after the slash) | ||
| − | -- to trigger use of { | + | -- to trigger use of {{hands/numdisp/fracparts}}. Hence, |
-- #if: {{#titleparts:{{{1|6+3/8}}}|1|2}} | -- #if: {{#titleparts:{{{1|6+3/8}}}|1|2}} | ||
-- will be true if a denominator exists (such as "8"). | -- will be true if a denominator exists (such as "8"). | ||
| + | -- | ||
| + | --NOTE N3: This template uses minimal expansion depth of the | ||
| + | -- if-else logic by checking for "/" before invoking the | ||
| + | -- nested subtemplate {{hands/numdisp/fracparts}}. The | ||
| + | -- check for a negative number increases the nesting depth | ||
| + | -- by 1 level to apply abs({1}) in a nested {#expr:} parser | ||
| + | -- function, to use the absolute value of the amount {1}. | ||
| + | -- | ||
| + | --NOTE Z4: Any end-zeroes on a decimal are preserved by not | ||
| + | -- using positive amounts in an expression, and for rare | ||
| + | -- negative amounts, the &minus with inverse -<nowiki/>{1} is padded | ||
| + | -- by {padright:} with end-zeroes "0" to {strlen_short} but | ||
| + | -- uses #ifexpr:{1}0={1} and floor({1})={1} to put ".". | ||
| + | -- So, {strlen_short} keeps minimal expansion depth of +2. | ||
-- | -- | ||
-- WARNING: *** DO NOT DELETE COMMENTS FROM THIS TEMPLATE *** | -- WARNING: *** DO NOT DELETE COMMENTS FROM THIS TEMPLATE *** | ||
| Line 38: | Line 73: | ||
--19Nov10 Put all parameters on 1 line to avoid newline "2^ /3" | --19Nov10 Put all parameters on 1 line to avoid newline "2^ /3" | ||
--20Nov10 Linked {Documentation} to hold any interwiki links. | --20Nov10 Linked {Documentation} to hold any interwiki links. | ||
| + | --12Feb11 Shows Unicode &minus ("−") for negative numbers. | ||
| + | --17Feb11 Used {padright:} to re-add end-zeroes on negatives. | ||
| + | --17Feb11 Used #ifexpr:{1}0={1} and floor({1})={1}, to put "." | ||
| + | --30Nov13 Fixed to allow prefix "−" as extra 6 long. | ||
| + | --07Jan14 Fixed to pass negative sum without padding "0". | ||
-- | -- | ||
** Add interwiki links to /doc subpage to avoid changes here ** | ** Add interwiki links to /doc subpage to avoid changes here ** | ||
| − | --> | + | -- |
| − | {{Documentation}} | + | -->{{Documentation}} |
</noinclude> | </noinclude> | ||
Latest revision as of 19:57, 11 May 2017
−14,000.
The Template:Hands/numdisp processes a number, for display, by checking to display a fraction part (at "/"), else it just uses parser-function {{formatnum:{{{1|6000}}}}} to add commas, such as to show "6,000".
- Usage: {{hands/numdisp|6+3/8}} → 6 3⁄8
- {{hands/numdisp|-4-1/2}} → −41⁄2
- {{hands/numdisp|23500}} → 23,500
- {{hands/numdisp|2300+643/1250}} → 2,300 643⁄1250
- {{hands/numdisp|−7.0}} → −7.
- {{hands/numdisp|14+6/ }} → Expression error: Unexpected < operator
Implementation
The check, for fraction parts, uses parser-function {{#titleparts:xx/xx|1|2}} to split a number at "/" (if any, as in: 6+3/8) and then checks if part 2 (the denominator, after the slash) contains a number, to trigger use of {{hands/numdisp/frac}}). Hence, the template uses:
- #if: {{#titleparts:{{{1|6+3/8}}}|1|2}}
That if-expression will be true if a denominator exists (such as "8" in 6+3/8).
See also
| The above documentation is transcluded from Template:Hands/numdisp/doc. (edit | history) Editors can experiment in this template's sandbox (edit | diff) and testcases (edit) pages. Please add categories to the /doc subpage. Subpages of this template. |