Module:Formatnum/doc

This module formats a number in the local format, based on MediaWiki data. This module is used by Template:Formatnum. For usage instructions, please take a look at Template:Formatnum.

Current limitations:

  • Numbers with more than 14 decimals are not supported.
  • It still does not allow more than 20 different "KnownLanguages" (in MediaWiki's core library for Lua) on one page. This means that you can have at a maximum 20 module transclusions, where each of them is formatting a number into a different language.

TODO:

Using this module from templates

edit

Usage:

{{formatnum|1=value|2=lang|prec=prec|sep=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 (as reported by MediaWiki's {{int:Lang}}) 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}}</nowiki> to pass indirectly its parameters to the main() function of this module in the parent frame.

formatNum()

edit

This function converts an value into a localized number.

Unlike "Language:formatNum()" in MediaWiki's core libraries for Lua, it correctly supports numbers using exponential notations such as 1e15 (MediaWiki's core function is currently broken and randomly forgets the exponent for some language, so that its formated numbers are incorrectly scaled and display wrong values).

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.