Jump to content

Extension:NumberFormat

From mediawiki.org
MediaWiki extensions manual
NumberFormat
Release status: stable
Implementation Parser function
Description Allows to format numbers by inserting thousands separators or rounding to a given number of decimals
Author(s)
Maintainer(s) Pavel Astakhov
Latest version 0.9.0 (2017-07-19)
MediaWiki 1.23+
PHP 5.3+
Database changes No
License GNU Lesser General Public License 3.0 or later
Download Template:WikimediaDownload/gerritonly
Example test.foxway.org
Quarterly downloads 7 (Ranked 117th)
Translate the NumberFormat extension if it is available at translatewiki.net

The NumberFormat extension allows to format numbers by grouping thousands and/or specifying the number of decimals to be shown. Additionally the number will be rounded if necessary. The symbols used as thousands separator and decimal point can optionally be specified.

This extension is most useful in conjunction with other extensions that print "raw" numbers — for example Semantic MediaWiki (see this tip on how to use NumberFormat with Semantic MediaWiki).

NumberFormat is a simple wrapper for the number_format() PHP function and uses the same default values.

Installation

[edit]
  • Download and move the extracted NumberFormat folder to your extensions/ directory.
    Developers and code contributors should install the extension from Git instead, using:cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/NumberFormat
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'NumberFormat' );
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Usage

[edit]
{{#number_format: number | decimals | dec_point | thousands_sep | orig_dec_point }}

where:

  1. number - the number being formatted (default empty)
  2. decimals - sets the number of decimal points (default "0", set "_" for do not round)
  3. dec_point - sets the separator for the decimal point (default ".")
  4. thousands_sep - sets the thousands separator (default ",", "n" = " " and "t" = " ")
  5. orig_dec_point - sets the separator for the decimal point in original number (default not set, if set - extension replace it to "." before use)


Example Result Explanation
{{#number_format:12345678.055555}} 12,345,678 Default behaviour: Round to whole number, insert ',' thousands separators
{{#number_format:12345678.055555|2}} 12,345,678.06 Round to two decimal places (useful for monetary amounts)
{{#number_format:12345678.055555|2|,|.}} 12.345.678,06 Round to two decimal places, use '.' as thousands separators and ',' as decimal point
{{#number_format:12345678.055555|2|,|_}} 12 345 678,06 Round to two decimal places, use a space as thousands separators and ',' as decimal point
{{#number_format:1.2345678055555e7|4}} 12,345,678.0556 Scientific notation is also accepted - here rounded to four decimal places
{{#number_format:12,345,678.055555}} 12,345,678 The original number must be a plain number, without thousands separators etc.
{{#number_format:12345678.055555|_|,|n}} 12 345 678,055555 No Round, use a non-break-space ( ) as thousands separators and ',' as decimal point
{{#number_format:12 345 678,055555|_|.||,}} 12345678.055555 Convert French user input to plain number (for example, use the user entered a number in parser function #expr.)

See also

[edit]