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

Free Wifi in Guildford

Peter Roome

I was asked by a friend today if I could recommend any bars/restaurants/cafés in Guildford where she could access free WiFi on her laptop. Besides Giraffe I wasn’t aware of anywhere else in town so I posted the question to Yammer in the office and received a number of help…

"DO NOT EAT" THROW AWAY

Steven Wake

I have the driest draw here at Kyan towers. You see, I am the proud owner of a Silica Gel collection. There is just something about them which compels me to not throw away the little fellas.

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…

Website easter egg

Piers Palmer

We decided to have a little fun now that summer is over, combining some design yumminess and behavioural goodness. See if you are up to the challenge! Can you find the indomitable and mighty web geek PROFESSOR WAKE on our website. He’s hiding there somewhere… A littl…