Merge pull request #15818 from colemanw/fields
[civicrm-core.git] / CRM / Core / Smarty / plugins / modifier.crmNumberFormat.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * Add thousands separator to numeric strings using
22 * PHP number_format() function.
23 *
24 * @param float $number
25 * Numeric value to be formatted.
26 * @param int $decimals
27 * Number of decimal places.
28 * @param string $dec_point
29 * Decimal point character (if other than ".").
30 * @param string $thousands_sep
31 * Thousands sep character (if other than ",").
32 *
33 * @return string
34 * the formatted string
35 *
36 * For alternate decimal point and thousands separator, delimit values with single quotes in the template.
37 * EXAMPLE: {$number|crmNumberFormat:2:',':' '} for French notation - 1234.56 becomes 1 234,56
38 */
39 function smarty_modifier_crmNumberFormat($number, $decimals = NULL, $dec_point = NULL, $thousands_sep = NULL) {
40 if (is_numeric($number)) {
41 // Both dec_point AND thousands_sep are required if one is not specified
42 // then use the config defaults
43 if (!$dec_point || !$thousands_sep) {
44 $config = CRM_Core_Config::singleton();
45 $dec_point = $config->monetaryDecimalPoint;
46 $thousands_sep = $config->monetaryThousandSeparator;
47 }
48
49 return number_format($number, $decimals, $dec_point, $thousands_sep);
50 }
51
52 return '';
53 }