Programmer's Reference Guide

Introduction to Zend_Currency

How to work with currencies

To use Zend_Currency within the own application just create an instance of it without any parameter. This will create an instance of Zend_Currency with the actual locale, and defines the currency which has to be used for this locale.

Example #1 Creating an instance of Zend_Currency from the actual locale

Expect you have 'en_US' set as actual locale through the users or your environment. By using no parameter while creating the instance you say Zend_Currency to use the actual currency from the locale 'en_US'. This leads to an instance with US Dollar set as actual currency with the formatting rules from 'en_US'.

<?php
require_once 'Zend/Currency.php';

$currency = new Zend_Currency();
        

Note: Be aware, that if your system has no default locale, or if the locale of your system can not be detected automatically, Zend_Currency will throw an exception. If you have this behaviour you should set the locale you manually.

Of course, depending on your needs, several parameters can be given at creation. Each of this parameters is optional and can be supressed. Even the order of the parameters can be switched. The meaning of each parameter is described in this list:

  • currency:

    A locale can include several currencies. Therefor the first parameter 'currency' can define which currency should be used by giving the short name of that currency. If that currency in not known in any locale an exception will be thrown. Currency short names are always 3 lettered and written uppercase. Well known currency shortnames are for example USD or EUR. For a list of all known currencies see the informational methods of Zend_Currency.

  • script:

    The second parameter 'script' defines in which script the output should be given. The standard script is 'Latn' which includes the global known digits from '0' to '9'. Other scripts like 'Arab' can include other digits. For more information about scripts see supported number scripts.

  • locale:

    The third parameter 'locale' defines which locale should be used for formatting the currency. The given locale will also be used to get the standard script and currency of this currency if these parameters are not given.

    Note: Note that Zend_Currency only accepts locales which include a region. This means that all given locale which only include the language will throw an exception. For example the locale en will throw an exception whereas the locale en_US will return USD as currency.

Example #2 Other examples for creating an instance of Zend_Currency

<?php
// expect standard locale 'de_AT'
require_once 'Zend/Currency.php';

// creates an instance from 'en_US' using 'USD', 'Latin' and 'en_US' as these are the default values from 'en_US'
$currency = new Zend_Currency('en_US');

// creates an instance from the actual locale ('de_AT') using 'EUR' as currency but displaying it in 'Arab' script
$currency = new Zend_Currency('Arab');

// creates an instance using 'EUR' as currency, 'en_US' for number formating and 'Arab' for the script
$currency = new Zend_Currency('en_US', 'EUR', 'Arab');
        

So you can supress any of these parameters if you want to use the default ones. This has no negative effect on handling the currencies. It can be usefull f.e. if you dont know the default currency for a region.

Note: For many countries there are several known currencies. One currency will actually be used and maybe several ancient currencies. If the 'currency' parameter is supressed the actual currency will be used. The region 'de' for example knows the currencies 'EUR' and 'DEM'... 'EUR' is the actual one and will be used if the parameter is supressed.

Create output from an currency

To get an existing value converted to a currency formatted output the method toCurrency() can be used. It takes a value which should be converted. The value itself can be any normalized number.

If you have a localized number you will have to convert it first to an normalized number with Zend_Locale_Format::getNumber(). Afterwards it can be used with toCurrency() to create an currency output.

toCurrency() accepts two optional parameters which can be used to temporary set another format.

  • script:

    The first parameter 'script' defines in which script the output should be given. The standard script is 'Latn' which includes the global known digits from '0' to '9'. Other scripts like 'Arab' can include other digits. For more information about scripts see supported number scripts. If this parameter is given, the given script is only used temporary for this method call.

  • locale:

    The second parameter 'locale' defines which locale should be used for formatting the currency. The given locale will also be used to get the standard script and currency of this currency if these parameters are not given. If this parameter is given, the given locale is only used temporary for this method call.

Example #3 Creating output for an currency

<?php
require_once 'Zend/Currency.php';

// creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US' as these are the default values from 'en_US'
$currency = new Zend_Currency('en_US');

// prints '$ 1,000.00'
echo $currency->toCurrency(1000);

// prints '$ 1.000,00'
echo $currency->toCurrency(1000, 'de_AT');

// prints '$ ١٬٠٠٠٫٠٠'
echo $currency->toCurrency(1000, 'Arab');
            

Changing the format of a currency

The format which is used by creation of a Zend_Currency instance is of course the standard format. But sometimes it is necessary to change this format for own purposes.

The format of an currency output includes the following parts:

  • Currency symbol, shortname or name:

    The symbol of the currency is normally displayed within an currency output. It can be supressed when needed or even overwritten.

  • Currency position:

    The position of the currency sign is normally automatically defined by the locale. It can be changed if necessary.

  • Script:

    The script which shall be used to display digits. Detailed information about scripts and their useage can be found in the documentation of Zend_Locale in supported number scripts.

  • Number formatting:

    The amount of currency (formally known as value of money) is formatted by the useage of formatting rules within the locale. For example is in english the ',' sign used as seperator for thousands, and in german the '.' sign.

So if you are in need to change the format, you can use the setFormat() method. If no parameter is given the standard settings from the actual or set locale is used. setFormat supports the following settings:

  • rules:

    This parameter selects what currency symbol or name should be displayed and where. Details are described below.

  • script:

    This parameter selects what script should be used for displaying digits. 'Latn' is the default script for most locales. But others like 'Arab' (arabian) are also supported.

  • locale:

    This parameter selects what locale should be used for formatting the numbers.

The 'rules' parameter accepts a string as input or any of the below described constants to define where the currency signs should be displayed and what to display in detail. A given string will override any previous set currency shortsign or currency name.

Constants for the selecting the currency description
constant value description
NO_SYMBOL 1 Do not display any currency representation
USE_SYMBOL 2 Display the currency symbol as currency description
USE_SHORTNAME 3 Display the 3 lettered currency abbreviation as currency description
USE_NAME 4 Display the currency name as currency description
Constants for the selecting the currency position
constant value description
STANDARD 8 Set the position of the description to standard as defined within the locale
RIGHT 16 Display the currency description at the right side of the currency value
LEFT 32 Display the currency description at the left side of the currency value

Example #4 Changing the displayed format of a currency

<?php
require_once 'Zend/Currency.php';

// creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US' as these are the default values from 'en_US'
$currency = new Zend_Currency('en_US');

// prints 'US$ 1,000.00'
echo $currency->toCurrency(1000);

$currency->setFormat(Zend_Currency::USE_NAME&Zend_Currency::RIGHT);
// prints '1.000,00 US Dollar'
echo $currency->toCurrency(1000);

$currency->setFormat("American Dollar");
// prints '1.000,00 American Dollar'
echo $currency->toCurrency(1000);
            

Informational methods for Zend_Currency

Of course, Zend_Currency supports also methods to get informations about any existing and many ancient currencies from Zend_Locale. The supported methods are:

  • getSymbol():

    Returns the known sign of the actual currency or a given currency. For example $ for the US Dollar within the locale 'en_US.

  • getShortName():

    Returns the abbreviation of the actual currency or a given currency. For example USD for the US Dollar within the locale 'en_US.

  • getName():

    Returns the full name of the actual currency of a given currency. For example US Dollar for the US Dollar within the locale 'en_US.

  • getRegionList():

    Returns a list of regions where the actual currency or a given one is known to be used. It is possible that a currency is used within several regions therefor the return value is always an array.

  • getCurrencyList():

    Returns a list of currencies which are known to be used in the given region.

The function getSymbol(), getShortName() and getName() accept two optional parameters. If no parameter is given the expected data will be returned from the actual set currency. The first parameter takes the shortname of a currency. Short names are always three lettered, for example EUR for euro or USD for US Dollar. The second parameter defines from which locale the data should be read. If no locale is given, the actual set locale is used.

Example #5 Getting informations from currencies

<?php
require_once 'Zend/Currency.php';

// creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US' as these are the default values from 'en_US'
$currency = new Zend_Currency('en_US');

// prints '$'
echo $currency->getSymbol();

// prints 'EUR'
echo $currency->getShortName('EUR');

// prints 'Österreichische Schilling'
echo $currency->getName('ATS', 'de_AT');

// returns an array with all regions where USD is used
print_r($currency->getRegionList();

// returns an array with all currencies which were ever used in this region
print_r($currency->getCurrencyList('de_AT');
            

Speed up Zend_Currency

The work of Zend_Currency can be speed up by the useage of Zend_Cache. By using the static method Zend_Currency::setCache($cache) which accepts one option, an Zend_Cache adapter. When you set it, the localization data of the methods from Zend_Currency are cached.


Introduction to Zend_Currency

Select a Version

Languages Available

Components

Search the Manual