You are here

public function DecimalFormatter::format in Currency 7.2

Overrides parent::format().

A decimal is formatted by splitting it into two integers: the major and minor unit. They are formatted individually and then joined together.

Parameters

float|string $number:

Overrides IntegerFormatter::format

1 call to DecimalFormatter::format()
CurrencyFormatter::format in currency/vendor/bartfeenstra/cldr/src/BartFeenstra/CLDR/CurrencyFormatter.php
Overrides parent::format().
1 method overrides DecimalFormatter::format()
CurrencyFormatter::format in currency/vendor/bartfeenstra/cldr/src/BartFeenstra/CLDR/CurrencyFormatter.php
Overrides parent::format().

File

currency/vendor/bartfeenstra/cldr/src/BartFeenstra/CLDR/DecimalFormatter.php, line 67
Contains class \BartFeenstra\CLDR\IntegerFormatter.

Class

DecimalFormatter
Formats a decimal according CLDR number pattern guidelines.

Namespace

BartFeenstra\CLDR

Code

public function format($number) {
  if ((double) $number != $number) {
    throw new \InvalidArgumentException('Number has no valid float value.');
  }
  $sign = (int) ($number < 0);

  // Split the number in major and minor units, and make sure there is a
  // minor unit at all.
  $number = explode('.', abs($number));
  $number += array(
    self::MINOR => '',
  );
  $digits = array(
    str_split($number[self::MAJOR]),
    strlen($number[self::MINOR]) ? str_split($number[self::MINOR]) : array(),
  );
  $symbols = $this
    ->cloneNumberPatternSymbols($this->symbols[$sign]);
  $this
    ->process($symbols[$sign][self::MAJOR], $digits[self::MAJOR]);

  // Integer formatting defaults from right to left, but minor units should
  // be formatted from left to right, so reverse all data and results.
  $symbols_minor = array_reverse($symbols[$sign][self::MINOR]);
  $this
    ->process($symbols_minor, array_reverse($digits[self::MINOR]));
  foreach ($symbols[$sign][self::MINOR] as $symbol) {
    if (!is_null($symbol->replacement)) {
      $symbol->replacement = strrev($symbol->replacement);
    }
  }

  // Prepare the output string.
  $output = array(
    self::MAJOR => '',
    self::MINOR => '',
  );
  foreach ($symbols[$sign] as $fragment => $fragment_symbols) {
    $this
      ->replacePlaceholders($fragment_symbols);
    foreach ($fragment_symbols as $symbol) {
      $output[$fragment] .= !is_null($symbol->replacement) ? $symbol->replacement : $symbol->symbol;
    }
  }
  return $output[self::MAJOR] . $this
    ->getReplacement(self::SYMBOL_SPECIAL_DECIMAL_SEPARATOR) . $output[self::MINOR];
}