You are here

function commerce_currency_format in Commerce Core 7

Formats a price for a particular currency.

Parameters

$amount: A numeric price amount value.

$currency_code: The three character code of the currency.

$object: When present, the object to which the price is attached.

$convert: Boolean indicating whether or not the amount needs to be converted to a decimal price amount when formatting.

Return value

A fully formatted currency.

17 calls to commerce_currency_format()
CommercePaymentTransactionEntityController::buildContent in modules/payment/includes/commerce_payment_transaction.controller.inc
Builds a structured array representing the entity's content.
CommercePaymentUITest::testCommercePaymentAdministration in modules/payment/tests/commerce_payment_ui.test
Test the adding payments using administration pages.
CommerceTaxUIAdminTest::testCommerceTaxUIAdminOrder in modules/tax/tests/commerce_tax_ui.test
Check the taxes applied in the order admin view.
CommerceTaxUIAdminTest::testCommerceTaxUIApplySalesTax in modules/tax/tests/commerce_tax_ui.test
Check if a 'Salex tax' rate is correctly applied in a given order.
CommerceTaxUIAdminTest::testCommerceTaxUIApplyVAT in modules/tax/tests/commerce_tax_ui.test
Check if a 'VAT' tax type is correctly applied in a given product.

... See full list

File

./commerce.module, line 630
Defines features and functions common to the Commerce modules.

Code

function commerce_currency_format($amount, $currency_code, $object = NULL, $convert = TRUE) {

  // First load the currency array.
  $currency = commerce_currency_load($currency_code);

  // Then convert the price amount to the currency's major unit decimal value.
  if ($convert == TRUE) {
    $amount = commerce_currency_amount_to_decimal($amount, $currency_code);
  }

  // Invoke the custom format callback if specified.
  if (!empty($currency['format_callback'])) {
    return $currency['format_callback']($amount, $currency, $object);
  }

  // Format the price as a number.
  $price = number_format(commerce_currency_round(abs($amount), $currency), $currency['decimals'], $currency['decimal_separator'], $currency['thousands_separator']);

  // Establish the replacement values to format this price for its currency.
  $replacements = array(
    '@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '',
    '@symbol_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol'] : '',
    '@price' => $price,
    '@symbol_after' => $currency['symbol_placement'] == 'after' ? $currency['symbol'] : '',
    '@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '',
    '@negative' => $amount < 0 ? '-' : '',
    '@symbol_spacer_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol_spacer'] : '',
    '@symbol_spacer' => $currency['symbol_placement'] == 'after' ? $currency['symbol_spacer'] : '',
    '@code_spacer' => $currency['code_spacer'],
  );

  // We switched from using trim() after token replacement because it couldn't
  // adequately trim non-breaking space characters while supporting certain
  // currency symbols like the GBP £. preg_replace() it slightly slower, but
  // the difference should be negligible on almost any site. If it becomes an
  // issue, we can introduce an if statement here to use trim() by default and
  // maintain an array of currency codes for which we must use preg_replace().
  $pattern = '/^\\s+|\\s+$/us';
  return preg_replace($pattern, '', check_plain(strtr('@code_before@code_spacer@negative@symbol_before@symbol_spacer_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements)));
}