Template:Convert/round/sandbox

0

The Template:Convert/round is a subtemplate of Template:Convert to round each converted amount, using the default rounding algorithm, when neither sigfig=n nor a rounding parameter (such as appending "|1") has been specified by the user. It displays the amount of parameter 2, rounded to the max of either the magnitude of parameter 2 or the precision of parameter 1 + parameter 5 - 0.5 + log 2 (as natural logarithm ln 2 / ln 10). The result is then formatted to add commas, if needed.

Within the formula to calculate the magnitude of the result, using logarithms, there is an adjustment to offset the computer truncation error in the digital storage. The trick to allow a one-line calculated magnitude is to add a truncation shift, such as tiny 1E-14 to fix the computer math, and then the formula becomes:
 * magnitude of x: {&#123;#expr: floor( (ln / ln 10) +1E-14 ) }}
 * where, log10x = ln x / ln 10 (so for 100, ln 100/ln10 = 2)

Thus, the template can get magnitude of 999,999 versus 1 million, or 1 billion, by tiny shift 1E-14 in the formula. Compare:
 * {&#123;Order_of_magnitude|999 999}} &rarr; 5
 * {&#123;#expr: floor( (ln 999999 / ln 10) +1E-14 ) }} =
 * {&#123;Order_of_magnitude|1 000 000}} &rarr; 6
 * {&#123;#expr: floor( (ln 1000000 / ln 10) +1E-14 ) }} =

The automatic rounding of the result can be extremely complicated for some decimal amounts, and so requires the above calculations to handle decimal precision.