Formatting a number as a currency value [duplicate]
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
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(".");
}
There is a plug-in for jQuery that formats to currencies: http://code.google.com/p/jquery-formatcurrency/
Demo available: Format currency demo
精彩评论