4 * Render a field as a numeric value
7 * - float: If true this field contains a decimal value. If unset this field
8 * will be assumed to be integer.
10 * @ingroup views_field_handlers
12 class views_handler_field_numeric
extends views_handler_field
{
13 function option_definition() {
14 $options = parent
::option_definition();
16 $options['set_precision'] = array('default' => FALSE
);
17 $options['precision'] = array('default' => 0);
18 $options['decimal'] = array('default' => '.', 'translatable' => TRUE
);
19 $options['separator'] = array('default' => ',', 'translatable' => TRUE
);
20 $options['prefix'] = array('default' => '', 'translatable' => TRUE
);
21 $options['suffix'] = array('default' => '', 'translatable' => TRUE
);
26 function options_form(&$form, &$form_state) {
27 parent
::options_form($form, $form_state);
29 if (!empty($this->definition
['float'])) {
30 $form['set_precision'] = array(
31 '#type' => 'checkbox',
32 '#title' => t('Round'),
33 '#description' => t('If checked, the number will be rounded.'),
34 '#default_value' => $this->options
['set_precision'],
36 $form['precision'] = array(
37 '#type' => 'textfield',
38 '#title' => t('Precision'),
39 '#default_value' => $this->options
['precision'],
40 '#description' => t('Specify how many digits to print after the decimal point.'),
41 '#process' => array('views_process_dependency'),
42 '#dependency' => array('edit-options-set-precision' => array(TRUE
)),
45 $form['decimal'] = array(
46 '#type' => 'textfield',
47 '#title' => t('Decimal point'),
48 '#default_value' => $this->options
['decimal'],
49 '#description' => t('What single character to use as a decimal point.'),
53 $form['separator'] = array(
54 '#type' => 'textfield',
55 '#title' => t('Thousands separator'),
56 '#default_value' => $this->options
['separator'],
57 '#description' => t('What single character to use as the thousands separator.'),
60 $form['prefix'] = array(
61 '#type' => 'textfield',
62 '#title' => t('Prefix'),
63 '#default_value' => $this->options
['prefix'],
64 '#description' => t('Text to put before the number, such as currency symbol.'),
66 $form['suffix'] = array(
67 '#type' => 'textfield',
68 '#title' => t('Suffix'),
69 '#default_value' => $this->options
['suffix'],
70 '#description' => t('Text to put after the number, such as currency symbol.'),
74 function render($values) {
75 $value = $values->{$this->field_alias
};
76 if (!empty($this->options
['set_precision'])) {
77 $value = number_format($value, $this->options
['precision'], $this->options
['decimal'], $this->options
['separator']);
80 $remainder = abs($value) - intval(abs($value));
81 $value = $value > 0 ?
floor($value) : ceil($value);
82 $value = number_format($value, 0, '', $this->options
['separator']);
84 // The substr may not be locale safe.
85 $value .
= $this->options
['decimal'] .
substr($remainder, 2);
89 // Check to see if hiding should happen before adding prefix and suffix.
90 if ($this->options
['hide_empty'] && empty($value) && ($value !== 0 || $this->options
['empty_zero'])) {
94 return check_plain($this->options
['prefix'] .
$value .
$this->options
['suffix']);