You are here

function uc_currency_format in Ubercart 7.3

Same name and namespace in other branches
  1. 8.4 uc_store/uc_store.module \uc_currency_format()
  2. 5 uc_store/uc_store.module \uc_currency_format()
  3. 6.2 uc_store/uc_store.module \uc_currency_format()

Formats an amount for display with the store's currency settings.

Parameters

$value: The numeric value of the currency amount.

$sign: The currency symbol. If FALSE is given, no symbol is used. The default, NULL, causes the variable 'uc_currency_sign' to be used, which defaults to '$'.

$thou: The thousands separator character. If FALSE is given, no separator is used. The default, NULL, causes the variable 'uc_currency_sign' to be used, which defaults to ','.

$dec: The decimal separator character. If FALSE is given, confusion will abound, because it will look 100 times bigger. The default, NULL, causes the variable 'uc_currency_dec' to be used, which defaults to '.'.

Return value

String containing price formatted with currency symbol and separators.

62 calls to uc_currency_format()
hook_uc_payment_entered in payment/uc_payment/uc_payment.api.php
Takes action when a payment is entered for an order.
template_preprocess_uc_order in uc_order/uc_order.module
Preprocesses a formatted invoice with an order's data.
test_gateway_charge in payment/uc_credit/tests/test_gateway.module
Callback function to perform the charge operation.
theme_uc_price in uc_store/uc_store.theme.inc
Displays a price in the standard format and with consistent markup.
UbercartAttributeCheckoutTestCase::testAttributeAddToCart in uc_attribute/tests/uc_attribute_checkout.test
Tests that product in cart has the selected attribute option.

... See full list

File

uc_store/uc_store.module, line 924
Contains global Ubercart functions and store administration functionality.

Code

function uc_currency_format($value, $sign = NULL, $thou = NULL, $dec = NULL) {
  if ($value === NULL) {
    return NULL;
  }
  $output = '';
  $sign_after = variable_get('uc_sign_after_amount', FALSE);
  $prec = variable_get('uc_currency_prec', 2);
  if (is_null($sign)) {
    $sign = variable_get('uc_currency_sign', '$');
  }
  if (is_null($thou)) {
    $thou = variable_get('uc_currency_thou', ',');
  }
  if (is_null($dec)) {
    $dec = variable_get('uc_currency_dec', '.');
  }

  // If the value is significantly less than the minimum precision, zero it.
  if ($prec > 0 && round(abs($value), $prec + 1) < pow(10, -$prec)) {
    $value = 0;
  }

  // Force the price to a positive value and add a negative sign if necessary.
  if ($value < 0) {
    $value = abs($value);
    $output .= '-';
  }

  // Add the currency sign first if specified.
  if ($sign && !$sign_after) {
    $output .= $sign;
  }

  // Format the number, like 1234.567 => 1,234.57
  $output .= number_format($value, $prec, $dec, $thou);

  // Add the currency sign last if specified.
  if ($sign && $sign_after) {
    $output .= $sign;
  }
  return $output;
}