Jump to content

User:Mattflaschen/decimal to binary

From Wikipedia, the free encyclopedia


Description

[edit]

This will convert a decimal numeral into a binary one. For example:

{{User:Superm401/decimal to binary|57}} produces: 1110012

and

{{User:Superm401/decimal to binary|4294967295}} produces: N/A2

Because templates do not allow internal variables, the template calculates every digit of the result independently then simply concatenates them together. This means the value of higher digits must always be subtracted using the mod operator. Then, the remaining value is divided by the appropriate power of 2, then truncated to 0 or 1. Because the truncation sometimes returns floating point values instead of 1 (e.g. .999999901), it was necessary to correct this by assigning either 1 or 0 with ifexpr, depending on whether the value is greater than 0.5.