src/i18n/Formatter.php
public function asCurrency($value, $currency = null, $options = [], $textOptions = [])
    {
        if ($value === null) {
            return $this->getNullDisplay();
        }
        
        $normalizedValue = $this->normalizeNumericValue($value);
        
        if ($this->isNormalizedValueMispresented($value, $normalizedValue)) {
            return $this->asCurrencyStringFallback((string) $value, $currency);
        }

        if ($this->_intlLoaded) {
            $currency = $currency ?: $this->currencyCode;
            // currency code must be set before fraction digits
            // http://php.net/manual/en/numberformatter.formatcurrency.php#114376
            if ($currency && !isset($textOptions[NumberFormatter::CURRENCY_CODE])) {
                $textOptions[NumberFormatter::CURRENCY_CODE] = $currency;
            }
            $formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions);
            if ($currency === null) {
                $result = $formatter->format($normalizedValue);
            } else {
                $result = $formatter->formatCurrency($normalizedValue, $currency);
            }
            if ($result === false) {
                throw new InvalidArgumentException('Formatting currency value failed: ' . $formatter->getErrorCode() . ' ' . $formatter->getErrorMessage());
            }

            return $result;
        }

        if ($currency === null) {
            if ($this->currencyCode === null) {
                throw new InvalidConfigException('The default currency code for the formatter is not defined and the php intl extension is not installed which could take the default currency from the locale.');
            }
            $currency = $this->currencyCode;
        }

        return $currency . ' ' . $this->asDecimal($normalizedValue, 2, $options, $textOptions);
    }
public function asCurrency($value, $currency = null, $options = [], $textOptions = [])
    {
        if ($value === null) {
            return $this->getNullDisplay();
        }
        
        $normalizedValue = $this->normalizeNumericValue($value);
        
        if ($this->isNormalizedValueMispresented($value, $normalizedValue)) {
            return $this->asCurrencyStringFallback((string) $value, $currency);
        }

        if ($this->_intlLoaded) {
            $currency = $currency ?: $this->currencyCode;
            // currency code must be set before fraction digits
            // http://php.net/manual/en/numberformatter.formatcurrency.php#114376
            if ($currency && !isset($textOptions[NumberFormatter::CURRENCY_CODE])) {
                $textOptions[NumberFormatter::CURRENCY_CODE] = $currency;
            }
            $formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions);
            if ($currency === null) {
                $result = $formatter->format($normalizedValue);
            } else {
                $result = $formatter->formatCurrency($normalizedValue, $currency);
            }
            if ($result === false) {
                throw new InvalidArgumentException('Formatting currency value failed: ' . $formatter->getErrorCode() . ' ' . $formatter->getErrorMessage());
            }

            return $result;
        }

        if ($currency === null) {
            if ($this->currencyCode === null) {
                throw new InvalidConfigException('The default currency code for the formatter is not defined and the php intl extension is not installed which could take the default currency from the locale.');
            }
            $currency = $this->currencyCode;
        }

        return $currency . ' ' . $this->asDecimal($normalizedValue, 2, $options, $textOptions);
    }
public function asCurrency ( $ value , $ currency = null , $ options = [ ] , $ textOptions = [ ] ) { if ( $ value === null ) { return $ this -> getNullDisplay ( ) ; } $ normalizedValue = $ this -> normalizeNumericValue ( $ value ) ; if ( $ this -> isNormalizedValueMispresented ( $ value , $ normalizedValue ) ) { return $ this -> asCurrencyStringFallback ( ( string ) $ value , $ currency ) ; } if ( $ this -> _intlLoaded ) { $ currency = $ currency ? : $ this -> currencyCode ; // currency code must be set before fraction digits // http://php.net/manual/en/numberformatter.formatcurrency.php#114376 if ( $ currency && ! isset ( $ textOptions [ NumberFormatter :: CURRENCY_CODE ] ) ) { $ textOptions [ NumberFormatter :: CURRENCY_CODE ] = $ currency ; } $ formatter = $ this -> createNumberFormatter ( NumberFormatter :: CURRENCY , null , $ options , $ textOptions ) ; if ( $ currency === null ) { $ result = $ formatter -> format ( $ normalizedValue ) ; } else { $ result = $ formatter -> formatCurrency ( $ normalizedValue , $ currency ) ; } if ( $ result === false ) { throw new InvalidArgumentException ( 'Formatting currency value failed: ' . $ formatter -> getErrorCode ( ) . ' ' . $ formatter -> getErrorMessage ( ) ) ; } return $ result ; } if ( $ currency === null ) { if ( $ this -> currencyCode === null ) { throw new InvalidConfigException ( 'The default currency code for the formatter is not defined and the php intl extension is not installed which could take the default currency from the locale.' ) ; } $ currency = $ this -> currencyCode ; } return $ currency . ' ' . $ this -> asDecimal ( $ normalizedValue , 2 , $ options , $ textOptions ) ; }
Formats the value as a currency number.

This function does not require the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed
to work, but it is highly recommended to install it to get good formatting results.

Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function
without PHP intl extension support. For very big numbers it's recommended to pass them as strings and not use
scientific notation otherwise the output might be wrong.

@param mixed $value the value to be formatted.
@param string $currency the 3-letter ISO 4217 currency code indicating the currency to use.
If null, [[currencyCode]] will be used.
@param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].
@param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]].
@return string the formatted result.
@throws InvalidArgumentException if the input value is not numeric or the formatting failed.
@throws InvalidConfigException if no currency is given and [[currencyCode]] is not defined.
Formats the value as a currency number .