yii\i18n\Formatter::asCurrency PHP Method

asCurrency() public method

This function does not require the PHP intl extension to be installed to work, but it is highly recommended to install it to get good formatting results.
public asCurrency ( mixed $value, string $currency = null, array $options = [], array $textOptions = [] ) : string
$value mixed the value to be formatted.
$currency string the 3-letter ISO 4217 currency code indicating the currency to use. If null, [[currencyCode]] will be used.
$options array optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].
$textOptions array optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]].
return string the formatted result.
    public function asCurrency($value, $currency = null, $options = [], $textOptions = [])
    {
        if ($value === null) {
            return $this->nullDisplay;
        }
        $value = $this->normalizeNumericValue($value);
        if ($this->_intlLoaded) {
            $formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions);
            if ($currency === null) {
                if ($this->currencyCode === null) {
                    if (($result = $formatter->format($value)) === false) {
                        throw new InvalidParamException('Formatting currency value failed: ' . $formatter->getErrorCode() . ' ' . $formatter->getErrorMessage());
                    }
                    return $result;
                }
                $currency = $this->currencyCode;
            }
            if (($result = $formatter->formatCurrency($value, $currency)) === false) {
                throw new InvalidParamException('Formatting currency value failed: ' . $formatter->getErrorCode() . ' ' . $formatter->getErrorMessage());
            }
            return $result;
        } else {
            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($value, 2, $options, $textOptions);
        }
    }

Usage Example

 public function testAsCurrency()
 {
     $value = '123';
     $this->assertSame('$123.00', $this->formatter->asCurrency($value));
     $value = '123.456';
     $this->assertSame("\$123.46", $this->formatter->asCurrency($value));
     // Starting from ICU 52.1, negative currency value will be formatted as -$123,456.12
     // see: http://source.icu-project.org/repos/icu/icu/tags/release-52-1/source/data/locales/en.txt
     //		$value = '-123456.123';
     //		$this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
 }
All Usage Examples Of yii\i18n\Formatter::asCurrency