class CRM_Utils_Money

Money utilties

Properties

static $_currencySymbols

Methods

static string
format(float $amount, string $currency = NULL, string $format = NULL, bool $onlyNumber = FALSE, string $valueFormat = NULL)

Format a monetary string.

static int
getCurrencyPrecision(string $currency = NULL)

This is a placeholder function for calculating the number of decimal places for a currency.

static float
subtractCurrencies($leftOp, $rightOp, $currency)

Subtract currencies using integers instead of floats, to preserve precision

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.

static string
formatLocaleNumeric($amount)

Format money for display (just numeric part) according to the current locale.

static string
formatLocaleNumericRounded(string $amount, int $numberOfPlaces)

Format money for display (just numeric part) according to the current locale with rounding.

static string
formatLocaleNumericRoundedByCurrency(string $amount, string $currency)

Format money for display (just numeric part) according to the current locale with rounding.

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.

static string
replaceCurrencySeparators(string $amount)

Replace currency separators.

static string
formatNumericByFormat(string $amount, string $valueFormat)

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

Parameters

float $amount The monetary amount to display (1234.56).
string $currency The three-letter ISO currency code ('USD').
string $format The desired currency format.
bool $onlyNumber
string $valueFormat The desired monetary value display format (e.g. '%!i').

Return Value

string formatted monetary string

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.

Parameters

string $currency

Return Value

int Number of decimal places.

at line 144
static float subtractCurrencies($leftOp, $rightOp, $currency)

Subtract currencies using integers instead of floats, to preserve precision

Parameters

$leftOp
$rightOp
$currency

Return Value

float Result of subtracting $rightOp from $leftOp to the precision of $currency

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

Parameters

$value1
$value2
$currency

Return Value

bool

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.

Parameters

$amount

Return Value

string

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.

Parameters

string $amount
int $numberOfPlaces

Return Value

string

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.

Parameters

string $amount
string $currency

Return Value

string Formatted amount.

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.

Parameters

$amount

Return Value

mixed

at line 251
static protected string replaceCurrencySeparators(string $amount)

Replace currency separators.

Parameters

string $amount

Return Value

string

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.

Parameters

string $amount
string $valueFormat

Return Value

string