javascript把数字每隔三位加逗号分开,JavaScript Number Format - Add Commas
来源:互联网 发布:软件系统流程图怎么画 编辑:程序博客网 时间:2024/04/30 11:08
Add Commas
JavaScript
function addCommas(nStr){nStr += '';x = nStr.split('.');x1 = x[0];x2 = x.length > 1 ? '.' + x[1] : '';var rgx = /(\d+)(\d{3})/;while (rgx.test(x1)) {x1 = x1.replace(rgx, '$1' + ',' + '$2');}return x1 + x2;}
Overview
Change a number such as 1000 into a string 1,000. Pass the value as a string, and it will preserve zeros.Examples
addCommas(1000)// 1,000addCommas(1231.897243)// 1,231.897243addCommas('9999999.00')// 9,999,999.00addCommas(-500000.99)// -500,000.99
Other separators
JavaScript
function addSeparatorsNF(nStr, inD, outD, sep){nStr += '';var dpos = nStr.indexOf(inD);var nStrEnd = '';if (dpos != -1) {nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);nStr = nStr.substring(0, dpos);}var rgx = /(\d+)(\d{3})/;while (rgx.test(nStr)) {nStr = nStr.replace(rgx, '$1' + sep + '$2');}return nStr + nStrEnd;}
Overview
addSeparatorsNF is part my comprehensive NumberFormat script, but if you only need separator formatting, then you can use the function by itself. It does not require the NumberFormat script.
Not every formatting style is the same. e.g. 1000 may be formatted as 1,000 or 1.000
So addSeparatorsNF gives you the ability to specify the input decimal character, the output decimal character, and the output separator character.
Arguments
To use addSeparatorsNF, you need to pass it the following arguments:
nStr: The number to be formatted, as a string or number. No validation is done, so don't input a formatted number. If inD is something other than a period, then nStr must be passed in as a string.
inD: The decimal character for the input, such as '.' for the number 100.2
outD: The decimal character for the output, such as ',' for the number 100,2
sep: The separator character for the output, such as ',' for the number 1,000.2
Examples
addSeparatorsNF(43211234.56, '.', '.', ',')// 43,211,234.56addSeparatorsNF('52093423.003', '.', ',', '.')// 52.093.423,003addSeparatorsNF('93432,8', ',', '.', ',')>// 93,432.8addSeparatorsNF('584,567890', ',', '.', ',')// 584.567890 addSeparatorsNF(-1.23e8, '.', '.', ',')// -123,000,000
Explanation
Code Explanation
The code starts off dividing the string into two parts (nStr and nStrEnd) if there is a decimal. A regular expression is used on nStr to add the commas. Then nStrEnd is added back. If the string didn't have nStrEnd temporarily removed, then the regular expression would format 10.0004 as 10.0,004
Regular Expression Explanation
\d+ in combination with \d{3} will match a group of 3 numbers preceded by any amount of numbers. This tricks the search into replacing from right to left.
- javascript把数字每隔三位加逗号分开,JavaScript Number Format - Add Commas
- javascript把数字每隔三位加逗号分开
- 每隔三位数字加逗号
- 常用的js处理方法(数字每隔三位加逗号分开,日期)
- java数字每隔三位加逗号
- 数字格式化,从右往左每隔三位加逗号(利用递归)
- js格式化数字(金额--每隔三位加逗号)
- 用正则表达式处理数字每隔三位加逗号逻辑
- 格式化金额,每隔三位加逗号
- JavaScript对INPUT输入的格式化显示 - 数字每隔3位加逗号
- 数字每三位加逗号
- 给数字每三位加一个逗号
- js 数字每个三位加一个逗号
- 数字每三位加逗号的方法
- js数字每个三位加一个逗号
- javascript 将长串数字每三个数字间加一个逗号
- ios 给数字每隔三位加一个,号
- 三位加逗号
- 编写高效的Android代码
- 我的vim配置
- 关于C++对象模型(下)
- 转新浪徐水峰博文-自定义WP7应用程序Title
- 使用DJ java Decomplier 将整个jar包反编译成源文件(转载)
- javascript把数字每隔三位加逗号分开,JavaScript Number Format - Add Commas
- pthread_kill 和 sigaction 函数
- C++中string相关函数用法
- 调用远程主机上的RMI服务时抛出java.rmi.ConnectException: Connection refused to host: 127.0.0.1异常原因及解决方案
- Groovy Goodness: Using ConfigSlurper with Configuration Scripts Groovy读取配置文件
- C# 委托与事件
- VS2010中的调试技巧 断点
- IE8下JS无法获得file控件全路径的解决办法
- Android开发——MediaProvider源码分析(1)