Difference between revisions of "Template:Hexadecimal"
imported>HJ Mitchell |
imported>Frietjes m (change nested ifeqs to switch statements and add option for "digit" which will just show the digit without the 0 padding or the subscripts) |
||
Line 1: | Line 1: | ||
− | + | {{#ifexpr: {{{1|0}}} > 268435455.9 or {{{1|0}}} < 0 |N/A|<!-- | |
− | -->{{#switch:{{#expr: ({{{1}}} div 16777216) mod 16}}|0={{#ifexpr: {{{1}}} >268435455 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- | + | -->{{#switch:{{#expr: ({{{1|0}}} div 16777216) mod 16}}|0={{#ifexpr: {{{1|0}}} >268435455 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- |
− | -->{{#switch:{{#expr: ({{{1}}} div 1048576) mod 16}}|0={{#ifexpr: {{{1}}} >16777215 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- | + | -->{{#switch:{{#expr: ({{{1|0}}} div 1048576) mod 16}}|0={{#ifexpr: {{{1|0}}} >16777215 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- |
− | -->{{#switch:{{#expr: ({{{1}}} div 65536) mod 16}}|0={{#ifexpr: {{{1}}} >1048575 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- | + | -->{{#switch:{{#expr: ({{{1|0}}} div 65536) mod 16}}|0={{#ifexpr: {{{1|0}}} >1048575 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- |
− | -->{{#switch:{{#expr: ({{{1}}} div 4096) mod 16}}|0={{#ifexpr: {{{1}}} >65535 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- | + | -->{{#switch:{{#expr: ({{{1|0}}} div 4096) mod 16}}|0={{#ifexpr: {{{1|0}}} >65535 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- |
− | -->{{#switch:{{#expr: ({{{1}}} div 256) mod 16}}|0={{#ifexpr: {{{1}}} >4095 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- | + | -->{{#switch:{{#expr: ({{{1|0}}} div 256) mod 16}}|0={{#ifexpr: {{{1|0}}} >4095 |0}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- |
− | -->{{#switch:{{#expr: ({{{1}}} div 16) mod 16}}|0={{#ifexpr: {{{1}}} >255 |0|{{# | + | -->{{#switch:{{#expr: ({{{1|0}}} div 16) mod 16}}|0={{#ifexpr: {{{1|0}}} >255 |0|{{#switch:{{{2}}}|no|none= 0}}}}|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- |
− | -->{{#switch:{{#expr: ({{{1}}} div 1) mod 16}}|0=0|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- | + | -->{{#switch:{{#expr: ({{{1|0}}} div 1) mod 16}}|0=0|1=1|2=2|3=3|4=4|5=5|6=6|7=7|8=8|9=9|10=A|11=B|12=C|13=D|14=E|15=F}}<!-- |
− | -->{{# | + | -->{{#switch:{{{2}}}|no|none|digit=|hex=<sub>hex</sub>|#default=<sub>16</sub>}}}}<noinclude> |
{{documentation}} | {{documentation}} | ||
</noinclude> | </noinclude> |
Revision as of 16:21, 4 May 2012
016
![]() | This template uses Lua: |
This template makes it easy to convert from decimal to hexadecimal.
Usage
Use: {{Hexadecimal|x}}
where x is the decimal number to be converted to a hexadecimal. Decimals and fractions will be rounded down. The number is, by default, formatted with a final subscript 16 to display the base. An optional second parameter of |hex
will replace the base with "hex".
To opt out of the subscript, use a second parameter of |no
(or equivalently |none
), which also forces the display of at least two hexadecimal digits (instead of just one for values lower than 16).
To show a single digit, without the subscript and without the 0 prefix, set the second parameter to |digit
.
Examples
To convert a number to a base 16:
{{Hexadecimal|0}}
→ 016{{Hexadecimal|15}}
→ F16{{Hexadecimal|3256}}
→ CB816
To convert a number to a base hex:
{{Hexadecimal|0|hex}}
→ 0hex{{Hexadecimal|15|hex}}
→ Fhex{{Hexadecimal|3256|hex}}
→ CB8hex
To display single digits:
{{Hexadecimal|0|digit}}
→ 0{{Hexadecimal|10|digit}}
→ A{{Hexadecimal|15|digit}}
→ F
To convert an RGB color from decimal code to hex code:
{{Hexadecimal|255|no}}{{Hexadecimal|165|no}}{{Hexadecimal|0|no}}
→ FFA500
- Not a number input
{{Hexadecimal|}}
→ Expression error: Unexpected > operator.{{Hexadecimal}}
→ 016{{Hexadecimal|foobar}}
→ Expression error: Unrecognized word "foobar".{{Hexadecimal|A B}}
→ Expression error: Unrecognized word "a".
See also
{{Dec2hex}}
The above documentation is transcluded from Template:Hexadecimal/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. |