CommerceGuys\Intl\Formatter\NumberFormatter::format PHP Method

format() public method

public format ( $value )
    public function format($value)
    {
        if (!is_numeric($value)) {
            $message = sprintf('The provided value "%s" must be a valid number or numeric string.', $value);
            throw new InvalidArgumentException($message);
        }
        // Ensure that the value is positive and has the right number of digits.
        $negative = bccomp('0', $value, 12) == 1;
        $signMultiplier = $negative ? '-1' : '1';
        $value = bcdiv($value, $signMultiplier, $this->maximumFractionDigits);
        // Split the number into major and minor digits.
        $valueParts = explode('.', $value);
        $majorDigits = $valueParts[0];
        // Account for maximumFractionDigits = 0, where the number won't
        // have a decimal point, and $valueParts[1] won't be set.
        $minorDigits = isset($valueParts[1]) ? $valueParts[1] : '';
        if ($this->groupingUsed) {
            // Reverse the major digits, since they are grouped from the right.
            $majorDigits = array_reverse(str_split($majorDigits));
            // Group the major digits.
            $groups = [];
            $groups[] = array_splice($majorDigits, 0, $this->primaryGroupSize);
            while (!empty($majorDigits)) {
                $groups[] = array_splice($majorDigits, 0, $this->secondaryGroupSize);
            }
            // Reverse the groups and the digits inside of them.
            $groups = array_reverse($groups);
            foreach ($groups as &$group) {
                $group = implode(array_reverse($group));
            }
            // Reconstruct the major digits.
            $majorDigits = implode(',', $groups);
        }
        if ($this->minimumFractionDigits < $this->maximumFractionDigits) {
            // Strip any trailing zeroes.
            $minorDigits = rtrim($minorDigits, '0');
            if (strlen($minorDigits) < $this->minimumFractionDigits) {
                // Now there are too few digits, re-add trailing zeroes
                // until the desired length is reached.
                $neededZeroes = $this->minimumFractionDigits - strlen($minorDigits);
                $minorDigits .= str_repeat('0', $neededZeroes);
            }
        }
        // Assemble the final number and insert it into the pattern.
        $value = strlen($minorDigits) ? $majorDigits . '.' . $minorDigits : $majorDigits;
        $pattern = $negative ? $this->negativePattern : $this->positivePattern;
        $value = preg_replace('/#(?:[\\.,]#+)*0(?:[,\\.][0#]+)*/', $value, $pattern);
        // Localize the number.
        $value = $this->replaceDigits($value);
        $value = $this->replaceSymbols($value);
        return $value;
    }

Usage Example

 /**
  * @covers ::isGroupingUsed
  * @covers ::setGroupingUsed
  *
  * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::__construct
  * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::format
  * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceDigits
  * @uses \CommerceGuys\Intl\Formatter\NumberFormatter::replaceSymbols
  * @uses \CommerceGuys\Intl\NumberFormat\NumberFormat
  */
 public function testGroupingUsed()
 {
     $numberFormat = $this->createNumberFormat($this->numberFormats['latn']);
     // The formatter groups correctly.
     $formatter = new NumberFormatter($numberFormat, NumberFormatter::DECIMAL);
     $this->assertTrue($formatter->isGroupingUsed());
     $this->assertSame('10,000.9', $formatter->format('10000.90'));
     // The formatter respects grouping turned off.
     $formatter = new NumberFormatter($numberFormat, NumberFormatter::DECIMAL);
     $formatter->setGroupingUsed(false);
     $this->assertFalse($formatter->isGroupingUsed());
     $this->assertSame('10000.9', $formatter->format('10000.90'));
 }