Javascript将数字转化成为货币格式字符串

 更新时间:2016年06月22日 11:43:56   作者:Eric Sun  
这篇文章主要介绍Javascript将数字转化成为货币格式字符串的方法,通俗易懂,需要的朋友可以参考下。

这里第一个方法是用JavaScript将数字number转换为货币字符串的格式(参数:保留小数位数,货币符号,整数部分千位分隔符,小数分隔符)

这里第二个方法是用简单的正则表达式将货币字符换转换为纯净的数字字符串,之后便可以将字符串转换为数字number
JavaScript Money Format(用prototype对Number进行扩展)

// Extend the default Number object with a formatMoney() method:
// usage: someVar.formatMoney(decimalPlaces, symbol, thousandsSeparator, decimalSeparator)
// defaults: (2, "$", ",", ".")
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) : "");
};

如下便是一些转换实例:

// Default usage and custom precision/symbol :
var revenue = 12345678;
alert(revenue.formatMoney()); // $12,345,678.00
alert(revenue.formatMoney(0, "HK$ ")); // HK$ 12,345,678

// European formatting:
var price = 4999.99;
alert(price.formatMoney(2, "€", ".", ",")); // €4.999,99

// It works for negative values, too:
alert((-500000).formatMoney(0, "£ ")); // £ -500,000

Currency to number – removing money formatting (用正则表达式进行过滤)

var price = (12345.99).formatMoney(); // "$12,345.99"

// Remove non-numeric chars (except decimal point/minus sign):
priceVal = parseFloat(price.replace(/[^0-9-.]/g, '')); // 12345.99

这个方法仅仅应用于小数分隔符为"."的模式,如果小数分隔符是"," 那么正则表达式为/[^0-9-,]/g

 不用prototype对Number进行拓展的版本:

// To set it up as a global 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) : "");
}

// To create it as a library method:
myLibrary.formatMoney = function (number, places, symbol, thousand, decimal) {
  /* as above */
}

// Example usage:
formatMoney(54321); // $54,321
myLibrary.formatMoney(12345, 0, "£ "); // £ 12,345

以上就是本文的全部内容,了解更多JavaScript的语法,大家可以查看:《JavaScript 参考教程》、《JavaScript代码风格指南》,也希望大家多多支持脚本之家。

相关文章

  • webpack4.x开发环境配置详解

    webpack4.x开发环境配置详解

    这篇文章主要介绍了webpack4.x开发环境配置方法,结合实例形式详细分析了webpack4.x的具体安装、项目创建、打包操作等相关问题与处理技巧,需要的朋友可以参考下
    2018-08-08
  • Bootstrap源码解读网格系统(3)

    Bootstrap源码解读网格系统(3)

    这篇文章主要源码解读了Bootstrap网格系统,介绍了Bootstrap网格系统的工作原理具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • 5个可以帮你理解JavaScript核心闭包和作用域的小例子

    5个可以帮你理解JavaScript核心闭包和作用域的小例子

    这篇文章主要介绍了5个可以帮你理解JavaScript核心闭包和作用域的小例子,本文是翻译自国外的一篇文章,短小精悍,需要的朋友可以参考下
    2014-10-10
  • JavaScript对象之深度克隆介绍

    JavaScript对象之深度克隆介绍

    这篇文章主要介绍了JavaScript对象之深度克隆介绍,本文详细的讲解了什么是对象深度克隆,并给出了示例代码,需要的朋友可以参考下
    2014-12-12
  • JavaScript闭包原理及作用详解

    JavaScript闭包原理及作用详解

    闭包是指内部函数总是可以访问其所在的外部函数中声明的变量和参数,即使在其外部函数被返回(寿命终结)了之后。这篇文章将为大家详细介绍一下闭包的原理,作用及用途,快来跟随小编一起学习一下吧
    2021-12-12
  • JavaScript中的alert()函数使用技巧详解

    JavaScript中的alert()函数使用技巧详解

    这篇文章主要介绍了JavaScript中的alert()函数使用技巧详解,本文讲解了普通弹出、带换行的文本、使用制表符、使用变量、使用样式等选择,需要的朋友可以参考下
    2014-12-12
  • js 获取元素在页面上的偏移量的方法汇总

    js 获取元素在页面上的偏移量的方法汇总

    javascript可以通过四个属性可以获得元素的偏移量,分别是1、offsetHeight,2、offsetWidth,3、offsetLeft,4、offsetTop,今天我们就来具体谈一下获取页面元素偏移量的最佳方法。
    2015-04-04
  • JavaScript中rxjs与 Observable 两大类操作符解析

    JavaScript中rxjs与 Observable 两大类操作符解析

    这篇文章主要介绍了JavaScript中rxjs与 Observable 两大类操作符解析,运算符是对 Observable 进行操作并返回 Observable 的函数,文章围绕主题展开详细内容,需要的小伙伴可以参考一下
    2022-07-07
  • JavaScript实现url地址自动检测并添加URL链接示例代码

    JavaScript实现url地址自动检测并添加URL链接示例代码

    写一个简单的聊天系统,发出Htpp的Url实现跳转加上a标签,下面是具体的实现,感兴趣的朋友不要错过
    2013-11-11
  • 微信小程序实现搜索关键词高亮的示例代码

    微信小程序实现搜索关键词高亮的示例代码

    这篇文章主要介绍了微信小程序实现搜索关键词高亮的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论