Working with currencies
- Posted by thomasw at 03:07:51 // //
- Announcements, Currency
Ever had to work with currencies in your application?
Well, then there are good news for you:
For Zend Framework 1.10 there is a completly rewritten implementation of Zend_Currency available. The two new main features are calculation and exchanging.
What is meant with these:
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.
$currency = new Zend_Currency( array( 'value' => 1000, 'currency' => 'EUR', ) ); $currency->add(4000)->div(5); print $currency;
As you can see the usage is quite simple…
‘value’ defines the actual amount (money value) this currency holds. When no value is given it defaults to 0. And for calculation there are several convinient methods. These include mathematical methods like:
add(), sub(), mul(), div() and mod().
And there are also comparing methods like:
compare(), equals(), isMore(), isLess()
All of them should provide you with all what you need to calculate currencies.
Until now it was quite simple.
But what should be done when you have to calculate different currencies.
Expect you have USD and EUR. What ever you want to do before processing calculation we need eighter to convert USD to EUR or EUR to USD. As the exchange rates differ from day to day it’s not possible to add fixed rates to Zend_Currency.
But also for this task there is a solution.
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.
See this simple class as example:
class MyService implements Zend_Currency_CurrencyInterface
{
public function getRate($from, $to)
{
// request the online service
// and return only a exchange rate for $from Currency to $to Currency
}
}
Now, to use this exchange service within Zend_Currency you can simply use the ’service’ property.
$currency_2 = new Zend_Currency( array( 'value' => 1000, 'currency' => 'USD', 'service' => 'MyService', ) ); $currency_2->add($currency);
What happens now in the last line from our example?
The add() method detcts that two different currencies are involved. USD and EUR. It will now call the getRate() method of our service to get an exchange rate from EUR to USD.
So what we get in return is “1000 USD + 1000 EUR”… when EUR have an exchange rate of 1.5 we would see 2.500 USD in return.
As you see the usage is really simple and now Zend_Currency is really a useful component for currency handling within your application.
Additionally I also reworked the manual for Zend_Currency completly… now there are 10 sections (instead of 2) with several small examples. It should be much more descriptive.
I hope you like this new (reworked) component.
Happy frameworking,
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
Add comment
Fill out the form below to add your own comments

Wednesday, January 6, 2010 - 22:25:00
Awesome!! I have nothing else to say but awesome!! And thank you very much for all of your tireless effort.
Thursday, January 7, 2010 - 12:41:09
nice work!!
Thursday, January 7, 2010 - 14:33:02
This is a really nice and useful addition to Zend_Currency! Thank you for your contributions to ZF!
Saturday, January 9, 2010 - 02:04:57
One question i see that method signature is:
public function getRate($from, $to)
But the rate depend on date and I don’t see date argument here. Is it mising or is not mandatory ? Thanks
Saturday, January 9, 2010 - 02:23:25
This is a information which the exchange service itself has to handle. As exchange rates to not change based on dates but on minutes (when you have the right service) you would have to request it anytime you request a rate.
The exchange service should use a cache. The time for which a rate is acceptable depends on the type of service you are connected with.
Therefor it is not part of the interface.
Sunday, January 10, 2010 - 23:15:09
Yes but what if i wont to convert two currencys with rate to a concrete date ? I’think that it’s reasonable request. I have plenty of reports where I must show amount in two curencys which one is main and the second one is using rate for a concrete date. For example simple report
Item Price Day
Car 10EUR(250CZK) 1.1.2010
Two days i want see the same amounts not something like these:
Item Price Day
Car 10EUR(280CZK) 1.1.2010
Because the transaction date is 1.1.2010
Anyway thanks for a good job.
And I’m sorry for my poor english.
Sunday, January 10, 2010 - 23:21:28
Only a very small amount of online services allow to return historic exchange rates.
There would be no benefit if we limit Zend_Currency to only this services. The given interface is the smalles available collection of informations.
As with all other components you are free to extend given classes to your own needs. Simply add your own interface which points to a date.
Tuesday, February 2, 2010 - 14:48:36
Hi,
I tried to implement this code in my website application. But I got a Zend currency exception ‘Currency ‘Array’ not found’. Then I tried to do this without exchange service. Still the same exception. But it will return string ‘US Dollar’ if i create the instance without any parameters. Do I need to make any other settings in my application to use ‘Zend currency’.
Please Help Me.
Thanks.
Aswathy
Wednesday, February 3, 2010 - 23:07:41
As noted in the blog this works only with ZF 1.10 and above. In earlier versions the array notation was not supported.
Having problems I would again ask to add a issue within Jira, ask in the mailinglist or within the IRC channels.
Thursday, June 3, 2010 - 17:10:07
Thomas your example wont work, because you need to provide an instance to the ’service’ option, otherwise the exception will be thrown ‘No exchange service applied’.
Thursday, June 3, 2010 - 18:06:35
My example works… take a look at “isCallable” within PHP’s manual.
You can call it with a string (function), an array (object/method), or with an instance.
Wednesday, June 16, 2010 - 02:19:57
This is some great help, you saved me some work, thanks
Thursday, July 8, 2010 - 10:23:18
The two country currencys with rate to a concrete date ? I’think that it’s reasonable request. I have plenty of reports where I must show amount in two curencys which one is main and the second one is using rate for a concrete date……..