You are here

function commerce_price_decimals_formatter_currency_format in Commerce Price Decimals Formatter 7

Formats a price for a particular currency.

Parameters

number $amount: A numeric price amount value.

string $currency_code: The three character code of the currency.

array $settings: Array with display settings.

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

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

Return value

string A fully formatted currency.

3 calls to commerce_price_decimals_formatter_currency_format()
commerce_price_decimals_formatter_field_formatter_view in ./commerce_price_decimals_formatter.module
Implements hook_field_formatter_view().
commerce_price_decimals_formatter_handler_area_line_item_summary_decimals::render in includes/views/handlers/commerce_price_decimals_formatter_handler_area_line_item_summary_decimals.inc
Render the area.
commerce_price_decimals_for_all_commerce_price_savings_formatter_prices_alter in modules/commerce_price_decimals_for_all.module
Implements hook_commerce_price_savings_formatter_prices_alter().

File

./commerce_price_decimals_formatter.module, line 297
Provides a display formatter for the price field in which you can specify the decimal places are displayed.

Code

function commerce_price_decimals_formatter_currency_format($amount, $currency_code, $settings, $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);
  }

  // Check the number of decimals.
  if (isset($settings['currencies'][$currency_code]['force']) && $settings['currencies'][$currency_code]['force'] == FALSE && $settings['currencies'][$currency_code]['decimals'] < $currency['decimals']) {
    $decimals = strlen(substr(strrchr($amount, "."), 1));
    if ($decimals < $settings['currencies'][$currency_code]['decimals']) {
      $decimals = $settings['currencies'][$currency_code]['decimals'];
    }
  }
  else {
    $decimals = $settings['currencies'][$currency_code]['decimals'];
  }
  if (isset($settings['currencies'][$currency_code]['zero']) && $settings['currencies'][$currency_code]['zero'] == TRUE) {
    $zeros = (int) substr(strrchr($amount, "."), 1);
    if ($zeros == 0) {
      $decimals = 0;
    }
  }

  // Alter currency decimals for a correct round.
  $currency['decimals'] = $decimals;

  // 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' => $currency['symbol_spacer'],
    '@code_spacer' => $currency['code_spacer'],
  );
  return trim(t('@code_before@code_spacer@negative@symbol_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements));
}