Extension:NumberFormat

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
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) Patrick Nagel, Pavel Astakhov
Latest version 0.6.0 (2012-10-25)
MediaWiki 1.15+
PHP 5.3+
Database changes no
License GNU Lesser General Public License
Download Template:WikimediaDownload/gerritonly
Example [1]
Hooks used
ParserFirstCallInit

Translate the NumberFormat extension if possible

Check usage and version matrix; code metrics

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 SMW).

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

Usage[edit | edit source]

{{#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)


Wiki text Result ver 0.4.1 Result ver 0.6.0 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}} First argument to number_format must be a number 12,345,678 The original number must be a plain number, without thousands separators etc.
{{#number_format:12345678.055555|_|,|n}} n/a 12 345 678,055555 No Round, use a non-break-space ( ) as thousands separators and ',' as decimal point
{{#number_format:12 345 678,055555|_|.||,}} n/a 12345678.055555 Convert French user input to plain number (for example, use the user entered a number in parser function #expr.)


Download[edit | edit source]

You can download the extension directly from the MediaWiki source code repository (browse code). You can get:

One of the extensions tags

Not all extensions have tags. Some extensions have tags for each release, in which case those tags have the same stability as the release. To download a tag

  • Go to the tags list
  • Click the name of the tag you want to download
  • Click "snapshot"
The latest version of one of the extensions branches

Each extension has a master branch containing the latest code (might be unstable). Extensions can have further branches as well.

  • Go to the branches list
  • Click the branch name
  • Click "snapshot"
A snapshot made during the release of a MediaWiki version.

This might be unstable and is not guaranteed to work with the associated MediaWiki version.

After you've got the code, save it into the extensions/NumberFormat directory of your wiki.

If you are familiar with git and have shell access to your server, you can obtain the extension, with all its tags and branches, as follows:

cd extensions
git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/NumberFormat.git

Installation[edit | edit source]

  • Download and extract the files in a directory called NumberFormat in your extensions/ folder. If you're a developer and this extension is in a Git repository, then instead you should clone the repository.
  • Add the following code at the bottom of your LocalSettings.php:
require_once( "$IP/extensions/NumberFormat/NumberFormat.php" );
  • Done! Navigate to "Special:Version" on your wiki to verify that the extension is successfully installed.

See also[edit | edit source]

  • Extension:Foxway, includes number_format, money_format and more than seventy functions for working with strings using the syntax of PHP.
  • Extension:FormatNum – Initial version with increased features