js将金额专成每隔3位数加逗号
js将金额专成每隔3位数加逗号,比如 12345.00 转成 12,345.00;
懒得解释具体代码如下
//分割
String.prototype.joinByNum = function(num, deli){
(typeof num == 'undefined' || num == 0 || num == '' ) && (num = 3);
(typeof deli == 'undefined' || deli == '') && (deli = ',');
var oristr = this.toString().replace(/^\s*|\s*$|¥|,/g, ''),//去除了一些特殊符号以及空格
i = oristr.length - 1,
lastIndex = oristr.lastIndexOf('.'),
str = '',
index = 0;
if(isNaN(oristr)) return oristr;
(lastIndex !== -1) && (i = lastIndex - 1);
for (; i >= 0; i--) {
str = oristr[i] + str;
if ( i !== 0 && ++index % num === 0) {
str = deli + str;
}
}
str += lastIndex !== -1 ? (function(){
return Number(oristr.substr(lastIndex)).toFixed(2).substr(1);
})() : '.00'; return str;
}; //钱分割
Number.prototype.joinByNum = function(){
return ''.joinByNum.apply(''+ this, arguments);
};
测试结果:
old:789456123.22
new:789,456,123.22
old:10154344
new:10,154,344.00
貌似num里有个方法,所以又改了下。注意:这个toLocaleString貌似有点问题。比如safari就不会转。
//钱分割
Number.prototype.fomatMoney = function(){
var str = this.toLocaleString(),
arr = str.split('.');
if (arr.length < 2) {
str = arr[0]+ '.00';
}else{
str = arr[0] + Number(arr[1]).toFixed(2).substr(1);
}
return str;
};
//钱分割
String.prototype.fomatMoney = function(){
var oristr = this.toString();
if(oristr.indexOf(',') !== -1) return oristr;
return (0).fomatMoney.apply(Number(oristr), arguments);
};
js将金额专成每隔3位数加逗号的更多相关文章
- js金额数字格式化实现代码(三位加逗号处理保留两位置小数)
工作中很常用的东西: 例1,使数字1111111变成11,111,111.00,保留两位小数. <html> <head> <script type="text ...
- 【Java】【14】从后往前每隔n位加逗号(用于货币)
1,String类型的数据 /** * @param strValue 待处理的数 * @param num 隔的位数 */ public static String separateStr(Stri ...
- js将金额转成大写金额
function Chinese(){ /* var num= $(dialogStruct.iframe.contentDocument.getElementById("contractA ...
- JS把数字金额转换成中文大写数字的函数
//把数字金额转换成中文大写数字的函数 function num2rmb ($num){ $c1="零壹贰叁肆伍陆柒捌玖"; $c2="分角元拾佰仟万拾佰仟亿" ...
- C#小写数字金额转换成大写人民币金额的算法
C#小写数字金额转换成大写人民币金额的算法 第一种方法: using System.Text.RegularExpressions;//首先引入命名空间 private string DaXie(st ...
- JavaScript将输入的数字金额转换成对应的中文大写金额
// 将输入的数字金额转换成对应的中文大写金额 // idNumber输入的数字金额,idCHN输出的中文大写金额 function TransformNumberIntoCHN(idNumber, ...
- PHP算法--将数字金额转换成大写金额
最近在看一些PHP算法题,遇到一个将数字金额转换成大写金额的小算法题,这里贴出自己的一个例子. 注:这个小算法适用于10万以内的金额. <?php //$num = 12345.67; func ...
- SQL函数:小写金额转换成大写
/********************************************************作者:版本:1.0创建时间:20020227修改时间:功能:小写金额转换成大写参数:n ...
- js将字符串转化成函数:eval(logOutCallbackFun+"()");
js将字符串转化成函数:eval(logOutCallbackFun+"()");
随机推荐
- GridView获取列子段的几种途径
GridView是ASP.NET中功能强大的数据显示控件,它的RowDataBound事件为我们提供了方便的控制行.列数据的途径. 要获取当前行的某个数据列,我在实践中总结有如下几种方法: 1. Ce ...
- Apache php Mysql部署(一)下载安装
前言 最近公司需要开发一个网站,但是又有特殊要求:不能使用java.只能在Windows平台部署.没方法,只能选择了Apache+php+Mysql的方案. 不知道有没有更好的,听所golang挺不错 ...
- C/C++技术常用网站
软件下载网站[visual studio 2005编译器] http://www.xdowns.com/ debug调试大牛 http://blogs.msdn.com/oldnewthing/ ht ...
- Scala编程--函数式对象
本章的重点在于定义函数式对象,也就是说,没有任何可变状态的对象的类.作为运行的例子,我们将创造若干把分数作为不可变对象建模的类的变体.在这过程中,我们会展示给你Scala面向对象编程的更多方面:类参数 ...
- elasticsearch rpm 安装
参考:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-repositories.html Dow ...
- 自定义一个只显示年月的DatePicker(UIDatePicker无法实现年月显示)
HooDatePicker 介绍(introduction) ==================================================项目需要一个DatePicker,只显 ...
- 搭建TestNG环境( 一)
一:搭建环境 打开eclipse-->help-->Install New SoftWare,按下图操作:添加http://beust.com/eclipse 验证是否安装成功,file- ...
- 2014年6月份第2周51Aspx源码发布详情
AMX高效自定义分页控件(WinForm)源码 2014-6-9 [VS2008]2014.6.9更新内容: 1. 更改用户自定义分页控件功能布局.大大精简了调用分页自定义控件的代码,和使用系统 ...
- 2016 - 1 - 25 CSS初步 (二)
1.The customising link We can change the link's style when we move our pointer on the link like that ...
- django文件批量上传-简写版
模板中创建表单 <form method='post' enctype='multipart/form-data' action='/upload/'> <input type='f ...