You are here

public function NumberFormatter::format in Physical Fields 8

Formats the given number for the current language.

Supported options:

  • use_grouping: Whether to use grouping separators, such as thousands separators. Default: true.
  • minimum_fraction_digits: Minimum fraction digits. Default: 0.
  • maximum_fraction_digits: Minimum fraction digits. Default: 6.

Parameters

string $number: The number.

array $options: The formatting options.

Return value

string The formatted number.

Overrides NumberFormatterInterface::format

File

src/NumberFormatter.php, line 47

Class

NumberFormatter
Default number formatter.

Namespace

Drupal\physical

Code

public function format($number, array $options = []) {
  $default_options = [
    'use_grouping' => TRUE,
    'minimum_fraction_digits' => 0,
    'maximum_fraction_digits' => 6,
  ];
  $options = array_replace($default_options, $options);
  if ($this->numberFormatter) {
    $this->numberFormatter
      ->setAttribute(\NumberFormatter::GROUPING_USED, $options['use_grouping']);
    $this->numberFormatter
      ->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $options['minimum_fraction_digits']);
    $this->numberFormatter
      ->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $options['maximum_fraction_digits']);
    $number = $this->numberFormatter
      ->format($number);
  }
  else {
    if ($options['minimum_fraction_digits'] == 0) {
      $number = Calculator::trim($number);
    }
  }
  return $number;
}