Global Lua Modules/Formatnum

It is used in:
* Template:Formatnum
This module formats numbers based on the formatting rules of each language.
== ==
== ==
: {{formatnum1=value2=langprec=precsep=compact}}
== Parameters ==
* lang — language code as a string (e.g. en, de, etc.). If the language is not specified (nil or empty string) or not supported, the current user's language will be used.
:: The lang named parameter is also a supported alias of the 2nd parameter.
:: The value "arabic-indic" is also currently supported as an alias, and replaced by a supported language code.
* See formatNum() below for the expected values and the description of other parameter values.
Note:
: This template internally uses {{#invoke:Formatnum|main}} to pass indirectly its parameters to the main() function of this module in the parent frame.
=== formatNum() ===
This function converts an value into a localized number.
Usage:
: formatted_string = formatnum.formatNum(value, lang, prec, compact)
Parameters:
* value — as an ASCII-only number or string. If the string cannot be converted to a number with Lua's tonumber(), that string will be returned as is.
* lang — language code as a string (e.g. en, de, etc.). If that language is not supported, localized digits and separators will not be used (except for a few languages).
* prec — if not nil and not negative, this is the number of digits in displayed decimals (by truncating the decimals in excess or by adding zeroes). Valid range: 0 to 14.
** when prec is not specified or nil, the decimal separator is shown only if there are 1 or more visible decimals;
** when prec is negative or non integer it is treated like nil;
** when prec is 0, there will never be any decimal separator or any displayed decimals;
** when prec is positive, a decimal separator will always be present before this number of decimals, but when prec is higher than 14, it is treated like 14.
* compact — if this option is not nil and not false, don't return any localized grouping separators (the localized decimal separator and digits are still used).
Examples:
* formatted_string = formatnum.formatNum(12345.123) — convert to user's language, using localized digits and separators
* formatted_string = formatnum.formatNum(12345.123,) — same thing (language code not supported)
* formatted_string = formatnum.formatNum(12345.123,'default') — same thing (language code not supported)
* formatted_string = formatnum.formatNum(12345.123,'en') — convert to English: "12,345.123"
* formatted_string = formatnum.formatNum(12345.123,'fr') — convert to French: "12 345,123"
* formatted_string = formatnum.formatNum(12345.123,'fr',2) — same thing but limit to 2 decimals: "12 345,12"
* formatted_string = formatnum.formatNum(12345,'fr',2) — same thing (2 null decimals are padded): "12 345,00"
* formatted_string = formatnum.formatNum(12345,'fr',2,true) — same thing but without grouping separators: "12345,00"
Limitations:
* Same limit of 20 "KnownLanguages" (because it still depends on mw.Language module in order to detect localized digits and separators).
* When specifying the "prec" parameter, decimals in excess are just truncated, and the least significant digit is not rounded.