function views_handler_field_math::render

3.x views_handler_field_math.inc views_handler_field_math::render($values)
2.x views_handler_field_math.inc views_handler_field_math::render($values)

Render the field.

Parameters

$values: The values retrieved from the database.

Overrides views_handler_field_numeric::render

File

handlers/views_handler_field_math.inc, line 38

Class

views_handler_field_math
Render a mathematical expression as a numeric value

Code

function render($values) {
  ctools_include('math-expr');
  $value = strtr($this->options['expression'], $this->get_render_tokens(array()));
  $expressions = explode(';', $value);
  $math = new ctools_math_expr;
  foreach ($expressions as $expression) {
    if ($expression !== '') {
      $value = $math->evaluate($expression);
    }
  }

  // The rest is directly from views_handler_field_numeric but because it
  // does not allow the value to be passed in, it is copied.
  if (!empty($this->options['set_precision'])) {
    $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
  }
  else {
    $remainder = abs($value) - intval(abs($value));
    $value = $value > 0 ? floor($value) : ceil($value);
    $value = number_format($value, 0, '', $this->options['separator']);
    if ($remainder) {
      // The substr may not be locale safe.
      $value .= $this->options['decimal'] . substr($remainder, 2);
    }
  }

  // Check to see if hiding should happen before adding prefix and suffix.
  if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
    return '';
  }

  return check_plain($this->options['prefix'] . $value . $this->options['suffix']);
}