You are here

protected function GeneralNumberFormatter::numberFormatBasic in Formatter Suite 8

Formats a number using the basic set of formatting features.

Basic number notation formats a number with two parts:

  • Whole part before the decimal.
  • Fractional part after the decimal.

The following formatting attributes are used:

  • Use thousands.
  • Decimal digits (decimal & float types only).

Parameters

mixed $number: The number to format.

string $preSuffix: (optional, default = '') The string to add before the suffix.

Return value

string The formatted number, including the prefix or suffix.

2 calls to GeneralNumberFormatter::numberFormatBasic()
GeneralNumberFormatter::numberFormat in src/Plugin/Field/FieldFormatter/GeneralNumberFormatter.php
Format a number using the current settings.
GeneralNumberFormatter::numberFormatPercentage in src/Plugin/Field/FieldFormatter/GeneralNumberFormatter.php
Formats a number using a basic set of formatting features for percentages.

File

src/Plugin/Field/FieldFormatter/GeneralNumberFormatter.php, line 1060

Class

GeneralNumberFormatter
Format a number field with a variety of notation styles and parameters.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

protected function numberFormatBasic($number, string $preSuffix = '') {

  // Get settings.
  $usePrefixAndSuffix = $this
    ->getSetting('usePrefixAndSuffix');
  $useThousands = $this
    ->getSetting('useThousands');
  $thousandsSeparator = $useThousands === TRUE ? ',' : '';
  if (is_int($number) === TRUE) {
    $decimalDigits = 0;
  }
  else {
    $decimalDigits = $this
      ->getSetting('decimalDigits');
  }

  // Format the number.
  $formatted = number_format($number, $decimalDigits, '.', $thousandsSeparator);

  // If needed, add the prefix and suffix.
  if ($usePrefixAndSuffix === FALSE) {
    return $formatted . $preSuffix;
  }
  $prefixes = $this
    ->getFieldPrefixes();
  $suffixes = $this
    ->getFieldSuffixes();
  $prefix = $this
    ->selectPrefix($number, $prefixes);
  $suffix = $this
    ->selectSuffix($number, $suffixes);
  return $prefix . $formatted . $preSuffix . $suffix;
}