开发者

Formatting a number as a currency value [duplicate]

开发者 https://www.devze.com 2023-03-14 23:20 出处:网络
This question already has answer开发者_如何学JAVAs here: Closed 11 years ago. Possible Duplicate:
This question already has answer开发者_如何学JAVAs here: Closed 11 years ago.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号