How to Work with CurrenciesTo use Zend_Currency within your application, create an instance of it without any parameters. This will create an instance of Zend_Currency with your locale set and defines the currency which should be used for this locale. Example #1 Creating an Instance of Zend_Currency from the Locale Assume you have 'en_US' set as the set locale by the user or your environment. By passing no parameters while creating the instance you tell Zend_Currency to use the currency from the locale 'en_US'. This leads to an instance with US Dollar set as the currency with formatting rules from 'en_US'.
Zend_Currency also supports the usage of an application-wide locale. You can set a Zend_Locale instance in the registry as shown below. With this notation you can avoid setting the locale manually for each instance when you want to use the same locale throughout the application.
Depending on your needs, several parameters can be speicified at instantiation. Each of these parameters is optional and can be omitted. Even the order of the parameters can be switched. The meaning of each parameter is described in this list:
Example #2 Other Ways to Create Instances of Zend_Currency
You can omit any of the parameters to Zend_Currency's constructor if you want to use the default values. This has no negative effect on handling the currencies. It can be useful if, for example, you don't know the default currency for a region.
Creating and Output String from a CurrencyTo get a numeric value converted to an output string formatted for the currency at hand, use the method toCurrency(). It takes the 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(). It may then be used with toCurrency() to create a currency output string. toCurrency(array $options) accepts an array with options which can be used to temporarily set another format or currency representation. For details about which options can be used see Changing the Format of a Currency. Example #3 Creating an Output String for a Currency
Changing the Format of a CurrencyThe format which is used by creation of a Zend_Currency instance is, of course, the standard format. But occasionally it is necessary to change this format. The format of an currency output includes the following parts:
So if you need to change the format, you should the setFormat() method. It takes an array which should include every option you want to change. The $options array supports the following settings:
Example #4 Changing the displayed format of a currency
Reference Methods for Zend_CurrencyOf course, Zend_Currency supports also methods to get information about any existing and many ancient currencies from Zend_Locale. The supported methods are:
The function getSymbol(), getShortName() and getName() accept two optional parameters. If no parameter is given the expected data will be returned from the 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 set locale is used. Example #5 Getting Information about Currencies
Settings new default valuesThe method setLocale() allows to set a new locale for Zend_Currency. All default values for the currency will be overwritten when this method is invoked. This includes currency name, abbreviation and symbol. Zend_Currency Performance OptimizationZend_Currency's performance can be optimized using Zend_Cache. The static method Zend_Currency::setCache($cache) accepts one option: a Zend_Cache adapter. If the cache adapter is set, the localization data that Zend_Currency uses are cached. There are some static methods for manipulating the cache: getCache(), hasCache(), clearCache() and removeCache().
|