Difference between revisions of "Template:Decimal Align"

From Team Fortress Wiki
Jump to: navigation, search
(Fix to styling with the Wiki upgrade)
m (Adding a PREFIX support)
 
(14 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{#if: {{{NUM|}}} |
+
<!-- This template creates a subtable with two columns, the left column contains the integer part, and the right column contains the decimal. -->
{{{!}} class="nopad" width="100%"
+
<includeonly>
{{!}}align="right" width="50%" style="border: 0 none; padding: 0;" {{!}} {{#expr: {{{NUM|1}}} mod ({{{NUM|1}}} + 1) }}
+
{| class="nopad" width="100%"
{{!}}align="left" width="50%" style="border: 0 none; padding: 0;" {{!}} {{#ifeq: 0 | {{{P|3}}} | | {{{SEP|{{#switch: {{SUBPAGENAME}}|ru|fr|de|pt|pl|ro|it|hu=,|#default=.}}}}}{{padleft:
+
| align="right" width="50%" style="border: 0 none; padding: 0; {{#switch:{{lc:{{{FORMAT|}}}}}|b=font-weight: bold;|i=font-style: italic;|bi|ib=font-weight: bold; font-style: italic;}}" | {{#if:{{{TOOLTIP|}}}|<span id="tooltip" style="cursor:help;border-bottom:1px dotted;" title="{{{TOOLTIP}}}">}}<!--
{{#expr: (({{{NUM|1}}} - ({{{NUM|1}}} mod ({{{NUM|1}}} + 1))) *
+
  In the left cell, right-aligned, we place the integer part:
{{#switch: {{{P|3}}}
+
  -->{{{PREFIX|}}}{{#expr: trunc({{{NUM|1}}}) }}<!--
| 1 = 10
+
-->{{#if:{{{TOOLTIP|}}}|</span>}}
| 2 = 100
+
| align="left" width="50%" style="border: 0 none; padding: 0; {{#switch:{{lc:{{{FORMAT|}}}}}|b=font-weight: bold;|i=font-style: italic;|bi|ib=font-weight: bold; font-style: italic;}}" | {{#if:{{{TOOLTIP|}}}|<span id="tooltip" style="cursor:help;border-bottom:1px dotted;" title="{{{TOOLTIP}}}">}}<!--
| 3 = 1000
+
  In the right cell, left-aligned, we place the decimal part:
| 4 = 10000
+
  -->{{#ifeq: {{{P|3}}} | 0
| 5 = 100000
+
    <!-- No padding, omit decimal part -->
| 6 = 1000000
+
    | {{ns:0}}
| 7 = 10000000
+
    <!-- Else, compute decimal part as an integer with round(num - floor(num), P) * 10^P -->
| 8 = 100000000
+
    | {{dec}}{{padleft:{{#expr: trunc((({{{NUM|1}}} - trunc {{{NUM|1}}}) round {{{P|3}}}) e {{{P|3}}})}}|{{{P|3}}}}}
| 9 = 1000000000
+
  }}{{{POSTFIX|}}}{{#if:{{{TOOLTIP|}}}|</span>}}
}}) mod (({{{NUM|1}}} - ({{{NUM|1}}} mod ({{{NUM|1}}} + 1))) *
+
|}</includeonly><noinclude>
{{#switch: {{{P|3}}}
+
{{clr}}
| 1 = 10
+
{{Documentation}}
| 2 = 100
 
| 3 = 1000
 
| 4 = 10000
 
| 5 = 100000
 
| 6 = 1000000
 
| 7 = 10000000
 
| 8 = 100000000
 
| 9 = 1000000000
 
}} + 1)
 
}}|{{{P|3}}}}} }}
 
{{!}}}
 
}}<noinclude>{{clr}}
 
{{Template doc}}
 
 
[[Category:Formatting templates|{{PAGENAME}}]]
 
[[Category:Formatting templates|{{PAGENAME}}]]
 
</noinclude>
 
</noinclude>

Latest revision as of 06:56, 13 March 2024


Template documentation [view] [edit] [history] [purge]

This template is useful for stacking values in a table, where the numbers should be aligned based on the decimal separator. This template takes three arguments:

Argument Meaning
NUM Number to format
P Number of places after the decimal separator, defaults to three.
PREFIX String to append to the start of the number, such as a ~ symbol.
POSTFIX String to append to the end of the number, such as a % symbol.
FORMAT B for Bold, I for Italic, and BI or IB for Bold Italic
TOOLTIP Hover tooltip for the entire number, including POSTFIX if present.

So in example, the following table construct:

{| class="wikitable grid"
|-
! class = "header" | Numbers
|-
| {{Decimal Align | NUM = 123.456789}}
|-
| {{Decimal Align | NUM = 123.456789 | P = 0}}
|-
| {{Decimal Align | NUM = 123}}
|-
| {{Decimal Align | NUM = {{#expr: 24 * 6 /11}}}}
|-
| {{Decimal Align | NUM = 999.1111 | P = 2}}
|-
| {{Decimal Align | NUM = 33.343 | POSTFIX = % | FORMAT = I}}
|-
| {{Decimal Align | NUM = 33.343 | PREFIX = ~ | FORMAT = B}}
|-
| {{Decimal Align | NUM = 33.343 | PREFIX = ~ | POSTFIX = % | FORMAT = I}}
|-
| {{Decimal Align | NUM = 33.343 | P = 0 | POSTFIX = &&&}}
|-
| {{Decimal Align | NUM = 19 | POSTFIX = % | FORMAT = BI}}
|-
| {{Decimal Align | NUM = 19 | POSTFIX = % | FORMAT = BI | TOOLTIP=tooltip}}
|}

Would generate:

Numbers
123 .457
123
123 .000
13 .091
999 .11
33 .343%
~33 .343
~33 .343%
33 &&&
19 .000%
19 .000%