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

formatCurrency() public method

public formatCurrency ( $value, CommerceGuys\Intl\Currency\CurrencyInterface $currency )
$currency CommerceGuys\Intl\Currency\CurrencyInterface
    public function formatCurrency($value, CurrencyInterface $currency)
    {
        // Use the currency defaults if the values weren't set by the caller.
        $resetMinimumFractionDigits = $resetMaximumFractionDigits = false;
        if (!isset($this->minimumFractionDigits)) {
            $this->minimumFractionDigits = $currency->getFractionDigits();
            $resetMinimumFractionDigits = true;
        }
        if (!isset($this->maximumFractionDigits)) {
            $this->maximumFractionDigits = $currency->getFractionDigits();
            $resetMaximumFractionDigits = true;
        }
        // Format the decimal part of the value first.
        $value = $this->format($value);
        // Reset the fraction digit settings, so that they don't affect
        // future formattings with different currencies.
        if ($resetMinimumFractionDigits) {
            $this->minimumFractionDigits = null;
        }
        if ($resetMaximumFractionDigits) {
            $this->maximumFractionDigits = null;
        }
        // Determine whether to show the currency symbol or the currency code.
        if ($this->currencyDisplay == self::CURRENCY_DISPLAY_SYMBOL) {
            $symbol = $currency->getSymbol();
        } else {
            $symbol = $currency->getCurrencyCode();
        }
        return str_replace('ยค', $symbol, $value);
    }

Usage Example

 /**
  * @covers ::getCurrencyDisplay
  * @covers ::setCurrencyDisplay
  * @covers ::formatCurrency
  *
  * @uses \CommerceGuys\Intl\Currency\Currency
  * @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 testCurrencyDisplay()
 {
     $numberFormat = $this->createNumberFormat($this->numberFormats['latn']);
     $currency = $this->createCurrency($this->currencies['USD']);
     // Currency display defaults to symbol.
     $formatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY);
     $this->assertSame(NumberFormatter::CURRENCY_DISPLAY_SYMBOL, $formatter->getCurrencyDisplay());
     $formattedNumber = $formatter->formatCurrency('100', $currency);
     $this->assertSame('$100.00', $formattedNumber);
     // Currency display respects setting the value to currency code.
     $formatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY);
     $formatter->setCurrencyDisplay(NumberFormatter::CURRENCY_DISPLAY_CODE);
     $this->assertSame(NumberFormatter::CURRENCY_DISPLAY_CODE, $formatter->getCurrencyDisplay());
     $formattedNumber = $formatter->formatCurrency('100', $currency);
     $this->assertSame('USD100.00', $formattedNumber);
 }