1

Possible Duplicate:
Format currency using javascript

I am returning data from the serverside as numeric values, i.e.

value[0] = 1
value[1] = 100
value[2] = 10000
value[3] = 1000000
value[4] = 100000000

and so on.

How do I format this at client side so they look like this:

£1
£100
£10,000
£1,000,000
£100,000,000
Community
  • 1
  • 1
nami
  • 1,186
  • 5
  • 19
  • 22
  • What are you using for formatting? Where do you need to do this formatting? Is it HTML, PHP, or what else? – Aleks G Jun 24 '11 at 11:04
  • What countries do you need this for? – Pekka Jun 24 '11 at 11:04
  • @Aleks G, Not sure what you mean by your first question. I need to apply the formatting to a variable before appending it to the screen. I am outputting html, and am using jquery.ajax to get json from the serverside, which sends back the raw values I need. – nami Jun 24 '11 at 11:15
  • @Pekka, I need this for `en-GB`. – nami Jun 24 '11 at 11:16

2 Answers2

5

You could try createing a function like this:

function Currency(sSymbol, vValue) {
  aDigits = vValue.toFixed(2).split(".");
  aDigits[0] = aDigits[0].split("").reverse().join("").replace(/(\d{3})(?=\d)/g,   "$1,").split("").reverse().join("");
  return sSymbol + aDigits.join(".");
}
JAiro
  • 5,914
  • 2
  • 22
  • 21
  • that works, but how do I remove the 2 decimal places at the end. I do not need those. – nami Jun 24 '11 at 11:22
  • @nami, just remove the number 2 in the second line. It should be vValue.toFixed().split("."); – JAiro Jun 24 '11 at 11:33
  • thanks, this solution seems to be more suited to my needs than the duplicate links, which are way too long winded. – nami Jun 24 '11 at 12:18
1

There is a plug-in for jQuery that formats to currencies: http://code.google.com/p/jquery-formatcurrency/

Demo available: Format currency demo

Peter Bridger
  • 9,123
  • 14
  • 57
  • 89