3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
37 class CRM_Utils_Money
{
38 static $_currencySymbols = NULL;
41 * Format a monetary string.
43 * Format a monetary string basing on the amount provided,
44 * ISO currency code provided and a format string consisting of:
46 * %a - the formatted amount
47 * %C - the currency ISO code (e.g., 'USD') if provided
48 * %c - the currency symbol (e.g., '$') if available
50 * @param float $amount
51 * The monetary amount to display (1234.56).
52 * @param string $currency
53 * The three-letter ISO currency code ('USD').
54 * @param string $format
55 * The desired currency format.
56 * @param bool $onlyNumber
57 * @param string $valueFormat
58 * The desired monetary value display format (e.g. '%!i').
61 * formatted monetary string
64 public static function format($amount, $currency = NULL, $format = NULL, $onlyNumber = FALSE, $valueFormat = NULL) {
66 if (CRM_Utils_System
::isNull($amount)) {
70 $config = CRM_Core_Config
::singleton();
73 $format = $config->moneyformat
;
77 $valueFormat = $config->moneyvalueformat
;
81 // money_format() exists only in certain PHP install (CRM-650)
82 if (is_numeric($amount) and function_exists('money_format')) {
83 $amount = money_format($valueFormat, $amount);
88 if (!self
::$_currencySymbols) {
89 self
::$_currencySymbols = CRM_Core_PseudoConstant
::get('CRM_Contribute_DAO_Contribution', 'currency', array(
90 'keyColumn' => 'name',
91 'labelColumn' => 'symbol',
96 $currency = $config->defaultCurrency
;
99 // money_format() exists only in certain PHP install (CRM-650)
100 // setlocale() affects native gettext (CRM-11054, CRM-9976)
101 if (is_numeric($amount) && function_exists('money_format')) {
102 $lc = setlocale(LC_MONETARY
, 0);
103 setlocale(LC_MONETARY
, 'en_US.utf8', 'en_US', 'en_US.utf8', 'en_US', 'C');
104 $amount = money_format($valueFormat, $amount);
105 setlocale(LC_MONETARY
, $lc);
109 ',' => $config->monetaryThousandSeparator
,
110 '.' => $config->monetaryDecimalPoint
,
113 // If it contains tags, means that HTML was passed and the
114 // amount is already converted properly,
115 // so don't mess with it again.
116 if (strpos($amount, '<') === FALSE) {
117 $amount = strtr($amount, $rep);
120 $replacements = array(
123 '%c' => CRM_Utils_Array
::value($currency, self
::$_currencySymbols, $currency),
125 return strtr($format, $replacements);
129 * This is a placeholder function for calculating the number of decimal places for a currency.
131 * Currently code assumes 2 decimal places but some currencies (bitcoin, middle eastern) have
132 * more. By using this function we can signpost the locations where the number of decimal places is
133 * currency specific for future enhancement.
135 * @param string $currency
138 * Number of decimal places.
140 public static function getCurrencyPrecision($currency = NULL) {
145 * Subtract currencies using integers instead of floats, to preserve precision
148 * Result of subtracting $rightOp from $leftOp to the precision of $currency
150 public static function subtractCurrencies($leftOp, $rightOp, $currency) {
151 if (is_numeric($leftOp) && is_numeric($rightOp)) {
152 $precision = pow(10, self
::getCurrencyPrecision($currency));
153 return (($leftOp * $precision) - ($rightOp * $precision)) / $precision;