You are here

function uc_currency_format in Ubercart 8.4

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

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

Parameters

float $value: The numeric value of the currency amount.

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

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

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

Return value

string String containing price formatted with currency symbol and separators.

64 calls to uc_currency_format()
AjaxTest::testCheckoutPaneAjax in uc_store/tests/src/FunctionalJavascript/AjaxTest.php
Tests Ajax on the checkout panes.
AttributeTest::testAttributeAddToCart in uc_attribute/tests/src/Functional/AttributeTest.php
Tests that product in cart has the selected attribute option.
AttributeTest::testAttributeUiProductAttributes in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the "product attributes" page.
AttributeTest::testAttributeUiProductOptions in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the "product options" page.
AttributeTest::testAttributeUiSettings in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the attribute settings page.

... See full list

File

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

Code

function uc_currency_format($value, $symbol = NULL, $thou = NULL, $dec = NULL) {
  if ($value === NULL) {
    return NULL;
  }
  $output = '';
  $config = \Drupal::config('uc_store.settings')
    ->get('currency');
  $symbol_after = $config['symbol_after'];
  $prec = $config['precision'];
  if (is_null($symbol)) {
    $symbol = $config['symbol'];
  }
  if (is_null($thou)) {
    $thou = $config['thousands_marker'];
  }
  if (is_null($dec)) {
    $dec = $config['decimal_marker'];
  }

  // 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 symbol first if specified.
  if ($symbol && !$symbol_after) {
    $output .= $symbol;
  }

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

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