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 Format numbers - insert thousands separators, round to a given number of decimals
Author(s) Patrick NagelTalk
Last version 0.4.1 (2011-05-11)
MediaWiki 1.15.1 (and probably earlier)
PHP >= 4
License LGPL
Download see below
Example [1]

Check usage (experimental)

Contents

[edit] What can this extension do?

Numbers can be formatted by grouping thousands and/or specifying the number of decimals to be shown, the number will be rounded when 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.

[edit] Usage

{{#number_format:number|decimals|dec_point|thousands_sep}}

Wiki text 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}} First argument to number_format must be a number The original number must be a plain number, without thousands separators etc.

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/NumberFormat/NumberFormat.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/NumberFormat/NumberFormat.php");

[edit] Code

<?php
 
$wgExtensionCredits['parserhook'][] = array(
'name'         => 'NumberFormat',
'version'      => '0.4.1', // 2011-05-11
'description'  => 'Format numbers: insert thousands separators, round to a given number of decimals',
'author'       => 'Patrick Nagel',
'url'          => 'http://www.mediawiki.org/wiki/Extension:NumberFormat',
);
 
$wgExtensionFunctions[] = 'number_format_Setup';
$wgHooks['LanguageGetMagic'][] = 'number_format_Magic';
 
function number_format_Setup() {
        global $wgParser;
        $wgParser->setFunctionHook('number_format', 'number_format_Render');
}
 
function number_format_Magic(&$magicWords, $langCode) {
        $magicWords['number_format'] = array(0, 'number_format');
        return true;
}
 
function number_format_Render(&$parser) {
        // {{#number_format:number|decimals|dec_point|thousands_sep}}
 
        // Get the parameters that were passed to this function
        $params = func_get_args();
        array_shift($params);
        $paramcount = count($params);
 
        if ($paramcount >= 1) {
                if ($params[0] == '') return '';
                if (!is_numeric($params[0])) return '<span class="error">First argument to number_format must be a number</span>';
        }
 
        switch ($paramcount) {
                case 4:
                        // Since 'space' cannot be passed through parser functions, users are advised to use
                        // the underscore instead. Converting back to space here.
                        if ($params[2] == '_') $params[2] = ' ';
                        if ($params[3] == '_') $params[3] = ' ';
                        return number_format($params[0], $params[1], $params[2], $params[3]);
                        break;
                case 3:
                        return '<span class="error">number_format needs one, two or four parameters - not three.</span>';
                        break;
                case 2:
                        return number_format($params[0], $params[1]);
                        break;
                case 1:
                        return number_format($params[0]);
                        break;
                case 0:
                        return "";
                        break;
                default:
                        return '<span class="error">wrong number of arguments to number_format.</span>';
        }
}

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox