js实现格式化金额,字符,时间的方法
本文实例讲述了js实现格式化金额,字符,时间的方法。分享给大家供大家参考。具体实现方法如下:
//金额转换成大写 functiontoDaX(num){ //金额大小写转换 if(isNaN(num)||num>Math.pow(10,12)) return""; varcn="零壹贰叁肆伍陆柒捌玖"; varunit=newArray("拾佰仟","分角"); varunit1=newArray("万亿",""); if(parseFloat(num)==0||num==''){ return"零圆"; } varnumArray=num.toString().split("."); varstart=newArray(numArray[0].length-1,2); functiontoChinese(num,index){ varnum=num.replace(/\d/g,function($1){ returncn.charAt($1)+unit[index].charAt(start--%4?start%4:-1); }); returnnum; } for(vari=0;i<numArray.length;i++){ vartmp=""; for(varj=0;j*4<numArray[i].length;j++){ varstrIndex=numArray[i].length-(j+1)*4; varstr=numArray[i].substring(strIndex,strIndex+4); varstart=i?2:str.length-1; vartmp1=toChinese(str,i); tmp1=tmp1.replace(/(零.)+/g,"零").replace(/零+$/,""); tmp1=tmp1.replace(/^壹拾/,"拾"); tmp=(tmp1+unit1[i].charAt(j-1))+tmp; } numArray[i]=tmp; } numArray[1]=numArray[1]?numArray[1]:""; numArray[0]=numArray[0]?numArray[0]+"元":numArray[0],numArray[1]=numArray[1].replace(/^零+/,""); numArray[1]=numArray[1].match(/分/)?numArray[1]:numArray[1]+"整"; returnnumArray[0]+numArray[1]; } /**金额格式化添加“,”分隔符*/
functionaddComma(money){ if(money==""){ return""; } if(money){ money=money.trim(); } if(/[^0-9\.\-\+]/.test(money)){ returnmoney; } money=parseFloat(money)+""; if('NaN'==money){ return "0.00"; } varmoney_flag=""; if(money.indexOf("-")!=-1){ money=money.replace(/-/g,""); money_flag="-"; }
money=money.replace(/^(\d*)$/,"$1."); money=(money+"00").replace(/(\d*\.\d\d)\d*/,"$1"); money=money.replace(".",","); varre=/(\d)(\d{3},)/; while(re.test(money)){ money=money.replace(re,"$1,$2"); } money=money.replace(/,(\d\d)$/,".$1"); varmoney= money_flag+""+money.replace(/^\./,"0.") returnmoney; }
/**金额格式化去“,”分隔符*/ functiondelComma(value){ varrtnVal=value+""; returnrtnVal.replace(/,/g,""); }
/** *金额格式化字符串添加小数点 */ functionaddPoint(money){ if(/[^0-9\.]/.test(money)){ returnmoney; } if(money.length<3||money.indexOf(".")>-1){ returnmoney; } returnmoney.substring(0,money.length-2)+"."+money.substring(money.length-2,money.length); } /** *金额格式化数字去小数点 */ functionremovePoint(money){ if(/[^0-9\.]/.test(money)){ returnmoney; } varvalueFloat=parseFloat(money)*100; varvalueInt=parseInt(valueFloat); returnvalueInt; } /*格式化小数点后两位数字以百分比显示*/ functionaddPercent(str){ varpercent=Math.floor(str*100)/100; percent=(percent.toFixed(2)); returnpercent+'%'; }
/**字符格式化添加空格分隔符*/ functionaddSpace(value){ if(value==null||value==""){ return""; }
varvalue=value+""; vartmpStr=""; while(value.length>4){ tmpStr=tmpStr+value.substring(0,4)+""; value=value.substring(4,value.length); } tmpStr=tmpStr+value; returntmpStr; }
/**字符格式化去除空格分隔符*/ functionremoveSpace(value){ varrtnVal=value+""; returnrtnVal.replace(//g,""); }
//格式化日期时间字符串 //YYYYMMDD-》YYYY-MM-DD //YYYYMMDDhhmmss-》YYYY-MM-DDhh:mm:ss functionformatDatetime(oldvalue){ if(oldvalue==null){ return""; }elseif(oldvalue.length==8){ returnoldvalue.substring(0,4)+ "-"+oldvalue.substring(4,6)+ "-"+oldvalue.substring(6,8); }elseif(oldvalue.length==14){ returnoldvalue.substring(0,4)+ "-"+oldvalue.substring(4,6)+ "-"+oldvalue.substring(6,8)+ ""+oldvalue.substring(8,10)+ ":"+oldvalue.substring(10,12)+ ":"+oldvalue.substring(12,14); }elseif(oldvalue.length==6){ returnoldvalue.substring(0,2)+ ":"+oldvalue.substring(2,4)+ ":"+oldvalue.substring(4,6); }else{ returnoldvalue; } }
functionStringToDate(str){ vardatainfo=str.split('-'); returnnewDate(datainfo[0],datainfo[1],datainfo[2]); }