Skip to content
Snippets Groups Projects

Remove trailing zeros

@@ -72,6 +72,7 @@ public static function defaultSettings() {
'thousand_separator' => '',
'decimal_separator' => '.',
'scale' => 2,
'remove_trailing_zeros' => FALSE,
'thousand_suffix' => 'K',
'million_suffix' => 'M',
'billion_suffix' => 'B',
@@ -108,6 +109,8 @@ public function getSetting($key) {
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$field_name = $this->fieldDefinition->getName();
$state_selector = ':input[name="fields[' . $field_name . '][settings_edit_form][settings][scale]"]';
$elements['decimal_separator'] = [
'#type' => 'select',
@@ -117,15 +120,33 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
',' => $this->t('Comma'),
],
'#default_value' => $this->getSetting('decimal_separator'),
'#states' => [
'invisible' => [
$state_selector => ['value' => '0'],
],
],
'#weight' => 5,
];
$elements['scale'] = [
'#type' => 'number',
'#title' => $this->t('Scale', [], ['context' => 'decimal places']),
'#min' => 0,
'#max' => 10,
'#default_value' => $this->getSetting('scale'),
'#description' => $this->t('The number of digits to the right of the decimal.'),
'#description' => $this->t('The number of digits to the right of the decimal marker.'),
'#weight' => 6,
];
$elements['remove_trailing_zeros'] = [
'#type' => 'checkbox',
'#title' => $this->t('Remove trailing zeros to the right of the decimal marker.'),
'#default_value' => $this->getSetting('remove_trailing_zeros'),
'#states' => [
'invisible' => [
$state_selector => ['value' => '0'],
],
],
'#weight' => 6,
];
@@ -181,26 +202,41 @@ public function settingsSummary() {
*/
protected function numberFormat($number) {
$decimal_separator = $this->getSetting('decimal_separator');
$formatted_number = '';
$scale = $this->getSetting('scale');
$setting_keys = ['thousand_suffix', 'million_suffix', 'billion_suffix', 'trillion_suffix'];
$suffix = '';
$suffixes = array_map([$this, 'getSetting'], $setting_keys);
$thousand_separator = $this->getSetting('thousand_separator');
if (is_numeric($number)) {
if ($number > 1000000000000) {
return number_format($number / 1000000000000, $scale, $decimal_separator, $thousand_separator) . $suffixes[3];
$formatted_number = number_format($number / 1000000000000, $scale, $decimal_separator, $thousand_separator);
$suffix = $suffixes[3];
}
elseif ($number > 1000000000) {
return number_format($number / 1000000000, $scale, $decimal_separator, $thousand_separator) . $suffixes[2];
$formatted_number = number_format($number / 1000000000, $scale, $decimal_separator, $thousand_separator);
$suffix = $suffixes[2];
}
elseif ($number > 1000000) {
return number_format($number / 1000000, $scale, $decimal_separator, $thousand_separator) . $suffixes[1];
$formatted_number = number_format($number / 1000000, $scale, $decimal_separator, $thousand_separator);
$suffix = $suffixes[1];
}
elseif ($number > 1000) {
return number_format($number / 1000, $scale, $decimal_separator, $thousand_separator) . $suffixes[0];
$formatted_number = number_format($number / 1000, $scale, $decimal_separator, $thousand_separator);
$suffix = $suffixes[0];
}
else {
$formatted_number = number_format($number, $scale, $decimal_separator, $thousand_separator);
}
if ($this->getSetting('remove_trailing_zeros') && $scale) {
// Remove the trailing zeros to the right of the decimal marker, if the
// formatter has been set to show decimals.
$formatted_number = rtrim(rtrim($formatted_number, '0'), $decimal_separator);
}
return number_format($number, $scale, $decimal_separator, $thousand_separator);
return $formatted_number . $suffix;
}
elseif (empty($number)) {
return '';
Loading