JS 将数字转化成为货币格式
最近由于项目的需要需要将数字format成货币格式,自己搞了半天效果不是很好,博客园有篇问题很好,再次转载记录一下
http://www.cnblogs.com/mingmingruyuedlut/archive/2013/05/19/3082177.html
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 实现数字格式化(货币格式)几种方法
// 方法一 function toThousands(num) { var result = [ ], counter = 0; num = (num || 0).toString().split( ...
- Js 将 Date 转化为指定格式的String
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- 【JS】 JS毫秒值转化为正常格式 或者正常格式转化为毫秒值
1.毫秒值转化为正常时间格式 最简单的方法 new Date(后台传来的毫秒值).toLocaleDateString() 就是这个样子 2.毫秒值转化为自定义的时间格式 本页面重写一下 toLo ...
- ruby中将数字转化为字符串格式时差
工作中有时候会碰到需要把数值展示成比较直观的时间差格式,divmod方法很适合做这个操作. divmod #输出商和余数的数组 60.divmod(50) #=> [1, 10 ...
- js中时间戳转化成时间格式
function formatDate(timestamp){ var test = new Date(parseInt(timestamp) * 1000); var $year = test.ge ...
- Js将数字转化为中文大写
function number_chinese(str) { var num = parseFloat(str); var strOutput = "", strUnit = '仟 ...
- js将时间戳转化为日期格式
function getLocalTime(nS) { var date = new Date(nS); var Y = date.getFullYear() + '-'; ...
- js将数字转换成货币形式的字符
因为UI图上有的地方需要将数字转成货币形式的,例如:1234567转成 1,234,567 这样的,不过之前没弄过,然后在网上搜了下方法,参考了下面这篇文章 参考文章:JS将数字转成货币形式的简单 ...
- js转换数据格式为货币格式
有时候输资金数据的时候如果位数较多就不好读了,如果输完能转换一下格式,转成用“,”隔开的通用格式就比较好看了.自己写了一个备用,以后用到的话就不用再写了. //将数字转换为货币格式,用,隔开 func ...
随机推荐
- Circular Buffer
From:http://bradforj287.blogspot.com/2010/11/efficient-circular-buffer-in-java.html import java.util ...
- [MySQL] 分页优化
在传统的分页思路影响下,很多人都形成了对于分页的固定理解,也就是给出select语句,先用count()函数计算出总的条目,除与每个页面大小pagesize,然后用ceil取整,得出总的页数,用lim ...
- CDR VBA鼠标选择
Dim x As Double, y As Double, Shift As Long, b As Boolean, doc As Document Dim sel1 As Shape, sel2 A ...
- 高可用thrift客户池的实现详解
最近,公司要求将组内的thrift客户端组件推广至公司内使用.基本的要求如下: 1.高可用 2.集成名称服务,也就配置文件支持服务发现 3.解耦,客户端和高可用组件解耦,简单来说就是,如果以后要切换其 ...
- ios app内嵌入http服务器
1.采用CocoaHTTPServer https://github.com/robbiehanson/CocoaHTTPServer 2.采用MongooseDaemon https://githu ...
- Ruby数组
Ruby数组是有序的,任何对象的整数索引的集合.每个数组中的元素相关联,并提取到的一个索引.下标与C或Java相似,从0开始.负数索引假设数组末尾,也就是说-1表示最后一个元素的数组索引,-2是数组中 ...
- 分享一个常用Adb命令
分享一个常用Adb命令 首先 首先感谢@xuxu的常用adb命令,收益良多,但是已经不能满足于我,所以补充了下. 再者 好久没发帖了,最近论坛老司机们都在讨论/总结,我就用这个干货回报吧. 最后 基于 ...
- php内置的数据结构函数使用小事例
1.栈数据结构 $stack = new splstack(); $stack->push("data1"); $stack->push("data2&quo ...
- 关于for循环------swift3.0
在程序开发当中,for循环使用的频率无疑是最高的.常用的swift循环是递增式遍历.当然各种循环,swift都能办到.但其大多采用关键字形式实现,大部分开发者更喜欢直接使用C式循环代码.在swift3 ...
- CSS 中Font Awesome 图标(附码表)
HTML中缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. Font Awesome 传送门:http://fontawesome.das ...