Extension:NumberFormat
From MediaWiki.org
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.8.0 (2014-06-16) | ||
MediaWiki | 1.19+ | ||
PHP | 5.3+ | ||
Database changes | No | ||
License | GNU Lesser General Public License | ||
Download | |||
Example | test.foxway.org | ||
|
|||
Translate the NumberFormat extension if it is available at translatewiki.net |
|||
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:
- number - the number being formatted (default empty)
- decimals - sets the number of decimal points (default "0", set "_" for do not round)
- dec_point - sets the separator for the decimal point (default ".")
- thousands_sep - sets the thousands separator (default ",", "n" = " " and "t" = " ")
- 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 | |
{{#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.) |
Installation[edit | edit source]
- Download and extract the tarball in your
extensions/
folder. It should generate a new folder calledNumberFormat
directly inside yourextensions/
folder. - 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:PhpTags Functions/Functions/String, includes
number_format
,money_format
and more than seventy functions for working with strings using the PHP syntax. - Extension:FormatNum – Initial version with increased features