JS 将数字转化成为货币格式
2017-01-20 20:51
726 查看
JavaScript Money Format(用prototype对Number进行扩展)
Number.prototype.formatMoney = function (places, symbol, thousand, decimal) { places = !isNaN(places = Math.abs(places)) ? places : 2; symbol = symbol !== undefined ? symbol : "$"; thousand = thousand || ","; decimal = decimal || "."; var number = this, negative = number < 0 ? "-" : "", i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "", j = (j = i.length) > 3 ? j % 3 : 0; return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : ""); };
示例代码
var revenue = 12345678; revenue.formatMoney(); // $12,345,678.00 revenue.formatMoney(0, "HK$ ");
// HK$ 12,345,678 // European formatting: var price = 4999.99; price.formatMoney(2, "€", ".", ",");
// €4.999,99 // It works for negative values, too: (-500000).formatMoney(0, "£ ");// £ -500,000
JavaScript function
function formatMoney(number, places, symbol, thousand, decimal) { number = number || 0; places = !isNaN(places = Math.abs(places)) ? places : 2; symbol = symbol !== undefined ? symbol : "$"; thousand = thousand || ","; decimal = decimal || "."; var negative = number < 0 ? "-" : "", i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "", j = (j = i.length) > 3 ? j % 3 : 0; return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : ""); }
示例代码
formatMoney(54321); // $54,321 formatMoney(12345, 0, "£ "); // £ 12,345 formatMoney(12345, 2, "£ "); // £ 12,345.00 formatMoney(12345.232, 2, "£ "); // £ 12,345.23
相关文章推荐
- JS 将数字转化成为货币格式
- JS将number数值转化成为货币格式
- js将number数值转化成为货币格式,货币格式化,金钱过滤器,货币过滤器,vue货币过滤金钱过滤全局和局部两种实现方式
- JS将number数值转化成为货币格式
- JS将number数值转化成为货币格式
- 用js实现下列功能,将给定的数字转化为千分位的格式,如把"10000"转化为"10,000",并考虑性能方面的因素
- JS如何将数字类型转化为没3个一个逗号的金钱格式
- js数字货币格式互转
- JS 将数字类型转化为没3个一个逗号的金钱格式
- js格式化货币,数字转货币格式。
- js数字的货币格式表示法(每三位一个逗号分隔)
- JS将浮点型数字转化为货币型格式
- JS如何将数字类型转化为没3个一个逗号的金钱格式
- 货币数字转化为大写格式
- Oracle中用pl/sql实现货币数字格式到汉字格式的转化
- Javascript中将输入的数字转化成货币格式
- js 将数字格式化成货币(钱)格式
- Android入门——数字组成的字符串转化为货币格式
- 用JS实现给定数字转换成千分位格式,如“10000”转化成“10,000”。
- Javascript中自动将输入的数字转化成货币格式