- orphan
The CurrencyFormat
view helper can be used to simplify rendering of localized currency values. It acts as a wrapper for the NumberFormatter
class within the Internationalization extension (Intl).
// Within your view
echo $this->currencyFormat(1234.56, 'USD', null, 'en_US');
// This returns: "$1,234.56"
echo $this->currencyFormat(1234.56, 'EUR', null, 'de_DE');
// This returns: "1.234,56 €"
echo $this->currencyFormat(1234.56, 'USD', true, 'en_US');
// This returns: "$1,234.56"
echo $this->currencyFormat(1234.56, 'USD', false, 'en_US');
// This returns: "$1,235"
echo $this->currencyFormat(12345678.90, 'EUR', true, 'de_DE', '#0.# kg');
// This returns: "12345678,90 kg"
echo $this->currencyFormat(12345678.90, 'EUR', false, 'de_DE', '#0.# kg');
// This returns: "12345679 kg"
currencyFormat(float $number [, string $currencyCode = null [, bool $showDecimals = null [, string $locale = null [, string $pattern = null ]]]])
Format a number
- param $number
The numeric currency value.
- param $currencyCode
(Optional) The 3-letter ISO 4217 currency code indicating the currency to use. If unset, it will use the default value
null
(getCurrencyCode()
).- param $showDecimals
(Optional) Boolean false as third argument shows no decimals. If unset, it will use the default value
true
(shouldShowDecimals()
).- param $locale
(Optional) Locale in which the currency would be formatted (locale name, e.g. en_US). If unset, it will use the default locale (
Locale::getDefault()
).- param $pattern
(Optional) Pattern string that is used by the formatter. If unset, it will use the default value
null
(getCurrencyPattern()
).- rtype
string
Set the currency code and the locale
The $currencyCode
and $locale
options can be set prior to formatting and will be applied each time the helper is used:
// Within your view
$this->plugin('currencyformat')->setCurrencyCode('USD')->setLocale('en_US');
echo $this->currencyFormat(1234.56);
// This returns: "$1,234.56"
echo $this->currencyFormat(5678.90);
// This returns: "$5,678.90"
setCurrencyCode(string $currencyCode)
The 3-letter ISO 4217 currency code indicating the currency to use
- param $currencyCode
The 3-letter ISO 4217 currency code.
- rtype
Zend\I18n\View\Helper\CurrencyFormat
setLocale(string $locale)
Set locale to use instead of the default
- param $locale
Locale in which the number would be formatted.
- rtype
Zend\I18n\View\Helper\CurrencyFormat
Show decimals
// Within your view
$this->plugin('currencyformat')->setShouldShowDecimals(false);
echo $this->currencyFormat(1234.56, 'USD', null, 'en_US');
// This returns: "$1,235"
setShouldShowDecimals(bool $showDecimals)
Set if the view helper should show two decimals
- param $showDecimals
Whether or not to show the decimals.
- rtype
Zend\I18n\View\Helper\CurrencyFormat
Set currency pattern
// Within your view
$this->plugin('currencyformat')->setCurrencyPattern('#0.# kg');
echo $this->currencyFormat(12345678.90, 'EUR', null, 'de_DE');
// This returns: "12345678,90 kg"
setCurrencyPattern(string $currencyPattern)
Set the currency pattern used by the formatter. (See the NumberFormatter::setPattern PHP method for more information.)
- param $currencyPattern
Pattern in syntax described in ICU DecimalFormat documentation
- rtype
Zend\I18n\View\Helper\CurrencyFormat