JS实现数字格式千分位相互转换方法
时间:2022-11-04 09:52:22|栏目:JavaScript代码|点击: 次
实例如下:
/** * 数字格式转换成千分位 *@param{Object}num */ function commafy(num){ if((num+"").Trim()==""){ return""; } if(isNaN(num)){ return""; } num = num+""; if(/^.*\..*$/.test(num)){ varpointIndex =num.lastIndexOf("."); varintPart = num.substring(0,pointIndex); varpointPart =num.substring(pointIndex+1,num.length); intPart = intPart +""; var re =/(-?\d+)(\d{3})/ while(re.test(intPart)){ intPart =intPart.replace(re,"$1,$2") } num = intPart+"."+pointPart; }else{ num = num +""; var re =/(-?\d+)(\d{3})/ while(re.test(num)){ num =num.replace(re,"$1,$2") } } return num; } /** * 去除千分位 *@param{Object}num */ function delcommafy(num){ if((num+"").Trim()==""){ return""; } num=num.replace(/,/gi,''); returnnum; }
栏 目:JavaScript代码
下一篇:JS访问SWF的函数用法实例
本文标题:JS实现数字格式千分位相互转换方法
本文地址:http://www.codeinn.net/misctech/218090.html