You are here

function units_mathematical_expression_format_as in Units of Measurement 7.2

Format one mathematical expression to the format of another.

Parameters

\UnitsMathematicalExpression $input: Mathematical expression to be formatted

\UnitsMathematicalExpression $output: Desired format for $input mathematical expression. Constants of this format will be filled in based on data stored in $input mathematical expression

Throws

UnitsMathematicalExpressionDimensionException Exception is thrown if the input and output mathematical expressions are of different physical dimension and therefore may not be treated on the same scale

1 call to units_mathematical_expression_format_as()
units_field_field_formatter_view in units_field/units_field.module
Implements hook_field_formatter_view().

File

./units.module, line 568
Provide API for managing and converting units of measurement.

Code

function units_mathematical_expression_format_as(UnitsMathematicalExpression $input, UnitsMathematicalExpression &$output) {
  if (!units_dimension_equal($input
    ->dimension(), $output
    ->dimension())) {
    throw new UnitsMathematicalExpressionDimensionException();
  }
  $quantity = $input
    ->evaluate();
  if (is_null($quantity)) {

    // Quantity would be null only in the case if $input mathematical expression
    // contains no dimensionless members. It could be the case if $input
    // mathematical expression is an atomic unit or such a composed unit that
    // decomposes into atomic ones without factors, i.e. the decomposition
    // happens only on the physical dimension level. For example, it could be
    // square meter (decomposed into "meter * meter") or alike.
    // To keep things simple for end users we assume "unit" to actually be
    // "1 * unit". That's why we plug in the quantity of 1.
    $quantity = 1;
  }
  $output = $output
    ->formatQuantity($quantity);
}