Module:Formatnum/doc
Appearance
This module is rated as ready for general use. It has reached a mature form and is thought to be bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
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:
- Improve these limits by not using mw.formatNum() — and fix the various bugs in that MediaWiki module (including missing or incorrect data for some languages).
- See /engines/LuaCommon/LanguageLibrary.php in Scribunto PHP library implementing the MediaWiki module.
- See /languages/Language.php in MediaWiki core library with the default support code for all known languages
- See /languages/classes/Language*.php in MediaWiki core library for replacement code specific to some languages
- See /resources/src/mediawiki.language/mediawiki.language.numbers.js for similar function in MediaWiki client-side JavaScript library
- Support more languages and localized digits (we need more complete mappings of languages to their numeric scripts for digits, more data for localized separators and the number of digits in groups).
- Properly handle rounding of the least significant figure when there are decimals in excess for the specified precision, instead of truncating them.
- Reference data:
- See By-Type Chart: Core Data: Numbering Systems in Unicode CLDR data (mappings of languages to numbering systems)
- /common/supplemental/numberingSystems.xml in Unicode CLDR data (digits and algorithms for each numbering system)
- Rule-Based Number Formatting in Unicode TR35: LDML (Locale Data Markup Language)
- icu::RuleBasedNumberFormat Class Reference: Detailed Description in ICU documentation, for the RBNF syntax used in CLDR data for implementing algorithmic number systems.
- See By-Type Chart: Numbers: Symbols in Unicode CLDR data (separators, signs, etc.)
- See By-Type Chart: Core Data: Number Formatting Patterns in Unicode CLDR data (formatting numbers using those numbering systems)
- Number Format Patterns in Unicode TR35: LDML (Locale Data Markup Language)
- See By-Type Chart: Core Data: Numbering Systems in Unicode CLDR data (mappings of languages to numbering systems)
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 separatorsformatted_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.