Merge pull request #23702 from colemanw/loadingPlaceholders
[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 */
17
18 /**
19 * Add thousands separator to numeric strings using
20 * PHP number_format() function.
21 *
22 * @param float $number
23 * Numeric value to be formatted.
24 * @param int $decimals
25 * Number of decimal places.
26 * @param string $dec_point
27 * Decimal point character (if other than ".").
28 * @param string $thousands_sep
29 * Thousands sep character (if other than ",").
30 *
31 * @return string
32 * the formatted string
33 *
34 * For alternate decimal point and thousands separator, delimit values with single quotes in the template.
35 * EXAMPLE: {$number|crmNumberFormat:2:',':' '} for French notation - 1234.56 becomes 1 234,56
36 */
37 function smarty_modifier_crmNumberFormat($number, $decimals = NULL, $dec_point = NULL, $thousands_sep = NULL) {
38 if (is_numeric($number)) {
39 // Both dec_point AND thousands_sep are required if one is not specified
40 // then use the config defaults
41 if (!$dec_point || !$thousands_sep) {
42 $config = CRM_Core_Config::singleton();
43 $dec_point = $config->monetaryDecimalPoint;
44 $thousands_sep = $config->monetaryThousandSeparator;
45 }
46
47 return number_format($number, $decimals, $dec_point, $thousands_sep);
48 }
49
50 return '';
51 }