Robin Whittleton

Currency conversion in JS

I recently had to make a couple of JavaScript currency conversion functions for a current project, so I thought I’d put them up here.

The project had a requirement for all currency values to be formatted as £xx,xxx.xx (commas to separate thousand blocks, and two digits of precision). The functions we came up with are:

function currency2number(currency) {
	return parseFloat(currency.slice(1).replace(/,/,''),10);
}
function number2currency(number) {
	number = Number(number).toFixed(2).split('.');
	number[0] = number[0].split("").reverse().join("");
	number[0] = number[0].match(/\d{1,3}/g).join(",");
	number[0] = number[0].split("").reverse().join("");
	return '£'+number[0] + '.' + number[1];
}

currency2number is simple enough – it removes the ‘£’ symbol and commas, and converts it from a string to a float. The only gotcha is passing in 10 as the radix; this prevents leading zeros causing the number to be parsed as octal, an old but recurrent JavaScript problem.

number2currency caused me a few more problems, the main one being that adding the comma separators has to start from the decimal point and work backwards. Let’s work through our solution:

  1. First, take the number, wrap with with a Number object and use the toFixed method to get the correct number of decimal places
  2. Next, split that number on the decimal place so we can work on the left hand side
  3. Add the commas. Because we need to start from the right and work backwards it’s easier just to reverse the string and work from the left. Unfortunately Javascript lacks a String.reverse function. Best we can do is Array.reverse, so: split the string into an array, reverse it, rejoin it, find blocks of three numbers and join them with commas, split into an array again, reverse back to normal and rejoin. Phew!
  4. Finally, simply tack on a £ symbol and join everything together

So there you go, hope this helps someone. If anyone’s got any better ways of doing this then definitely let us know.

Comments: 1

ape web design
commented on

I suppose in some sort of AJAX app this would be useful although I am not sure that this is the easiest way to do this or the right way either.

I suppose though that many web designers have different ways of doing things.

Add a comment

Note: comments are moderated before publication.

Most Popular

Cooliris and the 3D wall.

Paul Sturgess

Cooliris (formerly known as PicLens) is described by it’s developers as a “lightening fast ‘3D wall’ that lets you browse thousands of images, videos and more with ease.” Cooliris is installed as a browser add-on for Firefox, Safari or Internet Expl…

Kyan Jukebox Festive 50 2008

Gavin Shinfield

Alright pop pickers? Not ’arf! Well, it’s that time of year again, best of 2008 lists abound so I thought I’d put together some of the top tracks to be rocking the Kyan jukebox from this year’s releases and now that Saint John no longer graces our airwave…

Kyan.com design process

Lee Whitelock

It’s great when you get a project you can really sink your teeth into. We pride ourselves on the effort we put into all our projects, of course, but when it’s for your own agency you can really ‘go to town’ and try new things. Our current website was out…