User:ZabMilenko/formatnum

If you have used the ParserFunction called, you are already aware that it can format a number using commas and periods (or other symbols depending on locality). There is, however, a gotcha which can make &lt;ref&gt;&lt;/ref&gt; tags appear as a jumbled string of numbers and letters.

For an example of a this in action, take a look at this old revision from the English Wikipedia. The infobox fields pertaining to population contain some extraneous characters.

If you trace the problem to the source, you find that the infobox passes variables directly to formatnum like so:



The presence of a reference inside this formatnum construct will cause the jumbled text to appear, which is actually an anchor name to allow jumping from the reflist to the point in text where anchors like [1] appear. One example of this text looks like "UNIQa7f8ea221,597,034-ref-00,000,000-QINU". A page which had this problem all over due to a transclusion can be seen here.

One solution
A possible solution to the problem is to check for non-numeric variables before using the formatnum function. An example code to do this might be:



In this example, a simple test is performed by adding 1 to the value of varname. If that fails, the result is passed by text value. Otherwise, the number is formatted like normal.

Not every case will benefit from this workaround, but it should solve plenty of headaches related to references in templates.