You are here

protected function GeneralNumberFormatter::numberFormatNumeral in Formatter Suite 8

Formats a number using a number system base.

Numeral system formating always rounds floating-point values to an integer.

The following formatting attributes are used:

  • Number base.
  • Use zero padding.
  • Padding width.

Parameters

mixed $number: The number to format.

Return value

string The formatted number, including the prefix or suffix.

1 call to GeneralNumberFormatter::numberFormatNumeral()
GeneralNumberFormatter::numberFormat in src/Plugin/Field/FieldFormatter/GeneralNumberFormatter.php
Format a number using the current settings.

File

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

Class

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

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

protected function numberFormatNumeral($number) {

  // Get settings.
  $usePrefixAndSuffix = $this
    ->getSetting('usePrefixAndSuffix');
  $numberBase = $this
    ->getSetting('numberBase');
  $useZeroPadding = $this
    ->getSetting('useZeroPadding');
  $paddingWidth = $this
    ->getSetting('paddingWidth');

  // If negative, set a flag and make positive. Handle the negative later.
  $isNegative = FALSE;
  if ($number < 0) {
    $isNegative = TRUE;
    $number *= -1.0;
  }

  // Convert to integer.
  $number = round($number, 0);

  // Convert to the requested base. The result is a string.
  $formatted = base_convert($number, 10, $numberBase);

  // Handle padding.
  if ($useZeroPadding === TRUE) {
    $len = mb_strlen($formatted);
    if ($len < $paddingWidth) {
      $fmt = '%0' . $paddingWidth . 's';
      $formatted = sprintf($fmt, $formatted);
    }
  }

  // Add minus sign back in, if needed.
  if ($isNegative === TRUE) {
    $formatted = '-' . $formatted;
  }

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