最近由于项目的需要需要将数字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 将数字转化成为货币格式的更多相关文章

  1. js 实现数字格式化(货币格式)几种方法

    // 方法一 function toThousands(num) { var result = [ ], counter = 0; num = (num || 0).toString().split( ...

  2. Js 将 Date 转化为指定格式的String

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  3. 【JS】 JS毫秒值转化为正常格式 或者正常格式转化为毫秒值

    1.毫秒值转化为正常时间格式  最简单的方法 new Date(后台传来的毫秒值).toLocaleDateString() 就是这个样子 2.毫秒值转化为自定义的时间格式 本页面重写一下  toLo ...

  4. ruby中将数字转化为字符串格式时差

        工作中有时候会碰到需要把数值展示成比较直观的时间差格式,divmod方法很适合做这个操作.   divmod #输出商和余数的数组    60.divmod(50) #=> [1, 10 ...

  5. js中时间戳转化成时间格式

    function formatDate(timestamp){ var test = new Date(parseInt(timestamp) * 1000); var $year = test.ge ...

  6. Js将数字转化为中文大写

    function number_chinese(str) { var num = parseFloat(str); var strOutput = "", strUnit = '仟 ...

  7. js将时间戳转化为日期格式

    function getLocalTime(nS) {        var date = new Date(nS);        var Y = date.getFullYear() + '-'; ...

  8. js将数字转换成货币形式的字符

    因为UI图上有的地方需要将数字转成货币形式的,例如:1234567转成  1,234,567  这样的,不过之前没弄过,然后在网上搜了下方法,参考了下面这篇文章 参考文章:JS将数字转成货币形式的简单 ...

  9. js转换数据格式为货币格式

    有时候输资金数据的时候如果位数较多就不好读了,如果输完能转换一下格式,转成用“,”隔开的通用格式就比较好看了.自己写了一个备用,以后用到的话就不用再写了. //将数字转换为货币格式,用,隔开 func ...

随机推荐

  1. spring拦截器 实现应用之性能监控

    package cn.ximi.erp.web.common.interceptors; import cn.ximi.core.common.utils.string.StringUtil; imp ...

  2. The note of Vue.js

    In computed field, increment operator is not supported.

  3. 源码安装php

    php安装扩展源yum install epel-releaseyum updateyum install libmcrypt libmcrypt-devel mcrypt mhash -yyum i ...

  4. UIStackView before iOS9.0

    我用的Xcode8.1,同伴用的Xcode7.3.1,其上传了几个XIB文件,导致我这边项目一直爆红,爆红信息:"UIStackView before iOS9.0".如图: 网上 ...

  5. Js 日期 多少分钟前,多少秒前

    ;(function(window){ /** * [dateDiff 算时间差] * @param {[type=Number]} hisTime [历史时间戳,必传] * @param {[typ ...

  6. UML大战需求分析——阅读笔记06

    状态机图和活动图在样子比较相似,但状态机图是用来为对象的状态及造成状态改变的事件建模.我们大二学习UML统一建模语言状态机图模块时了解到,UML的状态机图主要用于建立对象类或对象的动态行为模型,描述系 ...

  7. Struts中文件上传的一些规则...

    1.action中定义规范 如果jsp中file的name="xxx",那么后台action中的属性要做相应更改为 private File xxx; private String ...

  8. Notepad++正则表达式语法

    \   转义字符  如:要使用 “\” 本身, 则应该使用“\\” \t  Tab制表符  注:扩展和正则表达式都支持 \r  回车符CR   注:扩展支持,正则表达式不支持 \n  换行符LF   ...

  9. JS 原型链

    之前对JS的prototype知识比较模糊,今天理清了记下来,以防忘记,直切正题: 1.要明白原型链,就必须先清楚JS的构造函数模式: js是面向对象的语言,既然是面型对象,就一定会有一个对象的模板, ...

  10. Java 反射调用动态方法

    package com.pigetest.util; import java.lang.reflect.Method; public class PrivateMethodTestHelper { p ...