You are here

function uc_length_format in Ubercart 8.4

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

Formats a length value for display.

Parameters

float $value: Numerical length value.

string $unit: Length unit. One of 'ft', 'in', 'cm', or 'mm', or NULL to use store default length units.

string $thou: The thousands separator character. If FALSE is given, no separator is used. The default, NULL, causes the configuration variable 'length.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 'length.decimal_marker' to be used, which defaults to '.'.

Return value

string String containing formatted length, including length units.

5 calls to uc_length_format()
Length::render in uc_store/src/Plugin/views/field/Length.php
Renders the field.
ProductTest::testProductNodeForm in uc_product/tests/src/Functional/ProductTest.php
Tests product node form.
ShipmentController::viewPackage in shipping/uc_fulfillment/src/Controller/ShipmentController.php
Displays the details of a package.
UcDimensionsFormatter::viewElements in uc_product/src/Plugin/Field/FieldFormatter/UcDimensionsFormatter.php
Builds a renderable array for a field value.
uc_product_tokens in uc_product/uc_product.tokens.inc
Implements hook_tokens().

File

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

Code

function uc_length_format($value, $unit = NULL, $thou = NULL, $dec = NULL) {
  $output = '';
  $config = \Drupal::config('uc_store.settings')
    ->get('length');
  $prec = $config['precision'];
  if (is_null($unit)) {
    $unit = $config['units'];
  }
  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 length to a positive value and add a negative sign if necessary.
  if ($value < 0) {
    $value = abs($value);
    $output .= '-';
  }

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

  // Add the units last.
  switch ($unit) {
    case 'in':
      return t('@valuein.', [
        '@value' => $output,
      ]);
    case 'ft':
      return t('@valueft.', [
        '@value' => $output,
      ]);
    case 'cm':
      return t('@valuecm', [
        '@value' => $output,
      ]);
    case 'mm':
      return t('@valuemm', [
        '@value' => $output,
      ]);
  }
}