CRM_Utils_Money
class CRM_Utils_Money
Money utilties
Properties
static | $_currencySymbols |
Methods
Format a monetary string.
This is a placeholder function for calculating the number of decimal places for a currency.
Subtract currencies using integers instead of floats, to preserve precision
Tests if two currency values are equal, taking into account the currency's precision, so that if the difference between the two values is less than one more order of magnitude for the precision, then the values are considered as equal. So, if the currency has precision of 2 decimal points, a difference of more than 0.001 will cause the values to be considered as different. Anything less than 0.001 will be considered as equal.
Format money for display (just numeric part) according to the current locale.
Format money for display (just numeric part) according to the current locale with rounding.
Format money for display (just numeric part) according to the current locale with rounding.
Format money for display (just numeric part) according to the current locale with rounding based on the default currency for the site.
Replace currency separators.
Format numeric part of currency by the passed in format.
Details
at line 64
static string
format(float $amount, string $currency = NULL, string $format = NULL, bool $onlyNumber = FALSE, string $valueFormat = NULL)
Format a monetary string.
Format a monetary string basing on the amount provided, ISO currency code provided and a format string consisting of:
%a - the formatted amount %C - the currency ISO code (e.g., 'USD') if provided %c - the currency symbol (e.g., '$') if available
at line 134
static int
getCurrencyPrecision(string $currency = NULL)
This is a placeholder function for calculating the number of decimal places for a currency.
Currently code assumes 2 decimal places but some currencies (bitcoin, middle eastern) have more. By using this function we can signpost the locations where the number of decimal places is currency specific for future enhancement.
at line 144
static float
subtractCurrencies($leftOp, $rightOp, $currency)
Subtract currencies using integers instead of floats, to preserve precision
at line 172
static bool
equals($value1, $value2, $currency)
Tests if two currency values are equal, taking into account the currency's precision, so that if the difference between the two values is less than one more order of magnitude for the precision, then the values are considered as equal. So, if the currency has precision of 2 decimal points, a difference of more than 0.001 will cause the values to be considered as different. Anything less than 0.001 will be considered as equal.
Eg.
1.2312 == 1.2319 with a currency precision of 2 decimal points 1.2310 != 1.2320 with a currency precision of 2 decimal points 1.3000 != 1.2000 with a currency precision of 2 decimal points
at line 194
static protected string
formatLocaleNumeric($amount)
Format money for display (just numeric part) according to the current locale.
This calls the underlying system function but does not handle currency separators.
It's not totally clear when it changes the $amount value but has historical usage.
at line 213
static protected string
formatLocaleNumericRounded(string $amount, int $numberOfPlaces)
Format money for display (just numeric part) according to the current locale with rounding.
At this stage this is conceived as an internal function with the currency wrapper functions determining the number of places.
This calls the underlying system function but does not handle currency separators.
It's not totally clear when it changes the $amount value but has historical usage.
at line 228
static string
formatLocaleNumericRoundedByCurrency(string $amount, string $currency)
Format money for display (just numeric part) according to the current locale with rounding.
This handles both rounding & replacement of the currency separators for the locale.
at line 240
static mixed
formatLocaleNumericRoundedForDefaultCurrency($amount)
Format money for display (just numeric part) according to the current locale with rounding based on the default currency for the site.
at line 251
static protected string
replaceCurrencySeparators(string $amount)
Replace currency separators.
at line 272
static protected string
formatNumericByFormat(string $amount, string $valueFormat)
Format numeric part of currency by the passed in format.
This is envisaged as an internal function, with wrapper functions defining valueFormat into easily understood functions / variables and handling separator conversions and rounding.