JS 金钱格式化
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时间戳格式化日期时间
由于mysql数据库里面存储时间存的是时间戳,取出来之后,JS要格式化一下显示.(李昌辉) 用的次数比较多,所以写了一个简单方法来转换: //时间戳转时间 function RiQi(sj) { va ...
- 161226、js日期格式化
JavaScript Date format(js日期格式化) 方法一:// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季 ...
- 【JavaScript】 knockout.js 日期格式化借助【momentjs】
源:Knockout.js 日期格式化 源:momentjs
- JS获取当前日期时间及JS日期格式化
Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...
- JS日期格式化(网上转载)
JS日期格式化(网上转载) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <ht ...
- js 日期格式化函数(可自定义)
js 日期格式化函数 DateFormat var DateFormat = function (datetime, formatStr) { var dat = datetime; var str ...
- Js/html格式化在线工具
Js/html格式化在线工具 Js/html格式化在线工具:http://tool.chinaz.com/Tools/jsformat.aspx
- js日期格式化 扩展Date()
javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H/h).分(m).秒(s ...
- js 时间格式化 (兼容safari)
js 时间格式化,兼容IE8和safari浏览器. function formatDate(date, fmt, near, type) { var dateStr = date; if (!date ...
随机推荐
- HCNA配置接口IP地址
1.拓扑图 2.R1配置 The device is running! <Huawei>sys <Huawei>system-view Enter system view, r ...
- 816 Ambiguous Coordinates (many cases problem)
https://www.cnblogs.com/Java3y/p/8846955.html -- link of the problem 816 IDEA: check the dot and int ...
- 【转载】#324 - A Generic Class Can Have More than One Type Parameter
A generic class includes one or more type parameters that will be substituted with actual types when ...
- Android 中间白色渐变到看不见的线的Drawable
用gradient <gradient android:startColor="#00ffffff" android:centerColor="#ffffff&qu ...
- bzoj1150 [CTSC2007]数据备份
Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中 ...
- 2018年第九届蓝桥杯【C++省赛B组】第三题 乘积尾零
如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零?5650 4542 3554 473 946 4114 3871 9073 90 43292758 7949 6113 5659 ...
- Java 字符串转码工具类
StringConvertUtils.java package javax.utils; /** * 字符串转码工具类 * * @author Logan * @createDate 2019-04- ...
- flexslider 图片轮播插件参数
$(window).load(function() { $('.flexslider').flexslider({ animation: "fade", //String: Sel ...
- 第26章 FMC—扩展外部SDRAM—零死角玩转STM32-F429系列
第26章 FMC—扩展外部SDRAM 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/ ...
- Java常见加密技术的密钥与加密串长度
Java常见的Java方式 1.Base64编码 2.十六进制(Hex)编码 3.MD消息摘要 4.DES加密 5.3DES加密 6.AES加密 6.RSA加密