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