将数字格式化为货币值 [英] Formatting a number as a currency value

查看:107
本文介绍了将数字格式化为货币值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用javascript设置货币格式

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

以此类推.

我该如何在客户端格式化它,使它们看起来像这样:

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(".");
}

这篇关于将数字格式化为货币值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆