User:Wikid77/numsense/doc

The User:Wikid77/numsense calculates the precision of a number (parameter 1) based on the number sense of how precise the amount has been specified. Contrary to typical engineering precision, where whole multiples of 10 are considered rounded to the nearest 10, this template treats the numbers 10, 20, 30 (etc.) as being rounded to the nearest 1 unit, as precision 0. Multiples of 100 are treated as rounded to nearest 10, with precision -1. Examples:
 * {&#123;numsense |23}}    &rarr;
 * {&#123;numsense |40}}    &rarr;
 * {&#123;numsense | 9}}    &rarr;
 * {&#123;numsense |10}}    &rarr;
 * {&#123;numsense |10 }}   &rarr;


 * {&#123;numsense |2300}} &rarr;
 * {&#123;numsense |67.00}} &rarr;
 * {&#123;numsense |67.000}} &rarr;
 * {&#123;numsense |59.004500}} &rarr;
 * {&#123;numsense |67000}} &rarr;


 * {&#123;numsense |-67}} &rarr;
 * {&#123;numsense |0.00}} &rarr;
 * {&#123;numsense |34.56E7}} &rarr;

In decimal precision, the number 10 is treated as between 9.5 < 10 < 10.5, rather than 5<10<15. Consequently, {numsense} gives 10, 40, 70 (etc.) the precision 0, same as the numbers 1-9. Also, numerals with trailing zeroes count the extra zeroes as higher precision, so 8.200 has precision 3 for the 3 decimal places.

Performance considerations
In runtime speed, User:Wikid77/numsense processes numbers at the rate of about 330 numbers per second. For most cases (0-99,000), the expansion depth is only 4 levels of the MediaWiki wp:expansion depth limit, but some numbers over 8 digits might use 6 levels. For simplicity in reviewing the basic algorithm, this template uses markup to handle small numbers, to avoid the complexity of a separate Lua script module using the Scribunto interface to pass data as frame arguments. However, for large amounts, the Lua-based {precision} is invoked from Module:Math.