Luma

Menu
  • Who Are We
  • What We Do
  • Our Work
  • Our Thoughts
  • Get in Touch
« Previous | Next »
Making Currency Conversion Easier for the Web

Making Currency Conversion Easier for the Web

Sunday, February 14th, 2010

Converting between different currencies is a common problem on the web. It turns up most frequently for ecommerce systems, but it tends to rear its head anytime money changes hands.

There’s a number of paid and free solutions out there, one of the best free ones I’ve come across is the one in Google Finance. Sometimes you want something you can integrate more closely, or modify more easily.

When providing currency conversion on a website, there’s a couple of common approaches:

  1. You could do the conversion by hooking into an external webservice and pulling back results in realtime everytime someone requests a conversion.
  2. You could display a conversion tool from another site via an iframe. The Google Finance tool I mentioned above is a good candidate for this.
  3. You could pull the exchange rates from an external source and generate a cached version on your server. This method is handy when you can’t afford (or don’t want) the performance hit of having to make a HTTP request everytime you perform conversion. Also, since you can actually access the raw data (again, without a performance hit) you can perform more complex financial calculations then are practical with the other methods.

I’d say number 2 is the most common for the simple case when you just want to allow the user to convert X dollars from currency Y to currency Z. It’s simple and straightforward. Number 3 is best when you want kickass performance, maximum control, or need to provide currency conversion in an environment where you can’t assume that Internet access will be available when the user is accessing your app.

Number 1 is a hydrid of 2 and 3 and has the downsides of both without really capitalising on their strengths. I don’t see much benefit to using that one.

The tool that I’ve written uses method 3. It’s written as a small command line tool. It generates small libraries in the desired target language to quickly calculate exchange rates and perform currency conversion. The library that it generates will only convert from a single currency (that you choose when you generate the library) but can convert currency amounts from that single currency to a host of others.

Helpfully, it can also generate tools in multiple programming languages. Although, it currently only supports Javascript and Ruby.

Why Should I Care?

If we wanted to generate a Javascript library to convert USD amounts to other currencies we could execute:

Code block   
generate_exchange_rates -c usd -f javascript

By default this will generate a Javascript file at the current path called “usd_exchange_rates.js”, which can be used from Javascript as follows:

Code block   
// What is the Base Currency of the converter?
exchange_rates.usd.base_currency();
// Get the list of all supported currencies:
exchange_rates.usd.supported_currencies();
// Get the exchange rate of the base currency to NZD.
// Basically how many NZD dollars does 1 USD buy:
exchange_rates.usd.get("NZD");
// Convert 2 USD to NZD:
exchange_rates.usd.convert(2, "NZD");
// Get the raw exchange rates for every supported currency:
exchange_rates.usd.rates();

And this all happens without server callbacks or messy iframes. Neat.

A Quick Word on Currency Sources

The tool can actually support a number of currency sources. It comes with out-of-the-box support for only one, the free exchange rate XML feed from the European Central Bank, but using custom sources is just a matter of writing a custom source class. You should be able to hook in most simple sources in less than 100 lines of Ruby code.

If anyone has any suggestions for sources that should be included in the default package I’d love to hear them.

How Would I Use This with My Shiny New Web App

Once the converter is generated it’s just a matter of linking (in the case of a Javascript converter) or requiring it (for a Ruby one) where you need it, and then using it like a normal library for that language. The generated converters have comments in the source and are quite short so you shouldn’t have any trouble figuring them out.

As for generating the converters, a periodic job running somewhere on the web server (CRON is your best friend here) that just generates a new version over the previous one is all you need. When generating new versions of Javascript converters you may have some caching issues to address before the new version will be loaded by the client’s browser. It just depends on how aggressively your framework is caching Javascript assets.

Issues?

There’s no known issues, there is a few rough edges however.

No Tests Yet

I put this together in one short, ultra fast coding session (just before lunch in fact) and I didn’t pause to write tests. They will come, but probably not immediately as it already works for my purposes.

How Formatters Work is Deeply Hideous

Yup, I’ve got a bunch of ideas how I could tidy this up. But it’s mainly to save me some embarrassment. It works fine, it’s just not pretty.

Enjoy!

If you have any feedback, or issues with this tool let me know and I’ll help where I can. I’d also like to thank Intercity who actually needed currency conversion and partially sponsored the development.

WE RECOMMEND:

  • Beginning Cross-Browser Testing of Websites

SEARCH

Search for this site

CONTACT

Rolly Fordham
rolly@luma.co.nz
+64-21-265-2749
Contact

MEET ME ON

  • twitter
  • LinkedIn

CODE

  • http://github.com/luma/
  • Exchange Rate Generator
  • Home
  • Who Are We
  • What We Do
  • Our Work
  • Our Thoughts
  • Get in Touch
© Luma Ltd 2010.