Zend_Currency

Easily Format and Output Currency Values with Zend Framework

Currency can be a pain. Different currency symbols, different formats, rounding, etc, etc. Zend makes it easy!

At it’s simplest, you can output formatted currency values as follows:

$currency = new Zend_Currency();
//prints 00.00 if user is in US
echo $currency->toCurrency(1000);

But Zend_Currency can do so much more than that. For example, it is compatible with Zend’s localization components, meaning that you can setup a Zend_Locale object once in your application, and Zend_Currency sees it and automatically performs the necessary formatting and conversions. For example, suppose you have an early running resource in your Zend MVC application that determines the appropriate locale for the application. In your bootstrap or resource file, you would have something like this:

//Detect user locale automatically
$locale = new Zend_Locale();

//Or pass a locale to use that locale
//$locale = new Zend_Locale('en_US');

//Save to registry
Zend_Registry::set('Zend_Locale', $locale);

Once you save the Zend_Locale object to the registry (using the key ‘Zend_Locale‘), all locale aware components of the Zend Framework will automatically use that locale for any conversions and translations.

The defaults will be sufficient for most uses, but many options are available to help you customize how Zend_Currency operates. For a full listing of options, please see the Zend Framework reference guide.

Calculating with Currencies

Zend_Currency allows you to do many calculation and comparisons with currencies, such as addition, subtraction, multiply, divide, modulo, and equality operations such as equals, greater than and less than.

For example, suppose we have a currency object with a starting value of 00. The easiest way to perform operations on our object is to use Zend_Currency‘s built in functions, like so:

$currency = new Zend_Currency(array('value' => 1000));

//Add 0 to the currency
$currency->add(100);

//Subtract 0 from the currency
$currency->sub(100);

In this way, you can easily perform arithmetic operations on your currency objects, without the need for first retrieving the value in the currency object, then performing the operation on the value, which would then be a number rather than a Zend_Currency object, which would remove all the handy formatting and conversion possibilities.

Exchange Rates

Zend Framework does not supply a way to convert currencies out of the box, but it does make quite easy to do so. Basically, you need to write a simple class that will talk to a conversion rate web service and then return the exchange rate to Zend_Currency, which handles all the math itself. For more information, please see the Zend Framework reference guide on exchange rates.

Performance

For most websites, performance will not be much of an issue as far as the Zend Framework is concerned. On websites that get a decent amount of traffic, however, the complicated nature of the Zend Framework can lead to decreased performance. Luckily, the authors of ZF have thoughtfully made many components caching enabled, meaning you can tell them to use a cache and they will automatically speed themselves up. Zend_Currency can use a cache object to increase performance. To use caching in Zend_Currency, simple set a cache object like so:

Zend_Currency::setCache($cache);

NOTE: the $cache object must be an instance of Zend_Cache. For more information on how to create cache objects with ZF, see How to use Memcache with the Zend Framework.

Author: nick
Source: ZendCoding

by News Robot on August 28, 2010 in News, No Comments »
tags:

Thomas Weidner’s Blog: Working with currencies

Zend_Currency component has been completely rewritten in Zend Framework 1.10. Thomas Weidner, author of new implementation, writes on his blog about new Zend_Currency features and gives few practical examples of component’s usage.

Zend_Currency is now able not only to render a currency but you can also calculate currency values with it. Several methods have been added to support you by this task. (…) As mentioned before Zend_Currency adds a new feature exchanging. This means that it allows to use Exchange Services. The idea behind is that there are several available online exchange services which can convert a currency into another. By using such a service Zend_Currency is able to convert currencies when needed.

Read the full story and see how new Zend_Currency component can help you in development of financial, e-commerce and similar applications.