js实现格式化金额,字符,时间的方法

复制代码 代码如下:

//金额转换成大写
     function toDaX(num){
        //金额大小写转换
           if (isNaN(num) || num > Math.pow(10, 12))
                    return "";
           var cn = "零壹贰叁肆伍陆柒捌玖";
           var unit = new Array("拾佰仟", "分角");
           var unit1 = new Array("万亿", "");
           if(parseFloat(num)==0||num==''){
               return "零圆";
           }
           var numArray = num.toString().split(".");
           var start = new Array(numArray[0].length - 1, 2);
           function toChinese(num, index) {
               var num = num.replace(/\d/g, function($1) {
                    return cn.charAt($1)+ unit[index].charAt(start-- % 4 ? start % 4 : -1);
                   });
               return num;
           }
           for ( var i = 0; i < numArray.length; i++) {
                var tmp = "";
                for ( var j = 0; j * 4 < numArray[i].length; j++) {
                   var strIndex = numArray[i].length - (j + 1) * 4;
                   var str = numArray[i].substring(strIndex, strIndex + 4);
                   var start = i ? 2 : str.length - 1;
                   var tmp1 = 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] + "整";
           return numArray[0] + numArray[1];
     } 
/** 金额格式化 添加“,”分隔符 */

function addComma(money) {
if(money==""){
return "";
}
if(money){
money = money.trim();
}
    if(/[^0-9\.\-\+]/.test(money)){  
    return money;
    }
    money = parseFloat(money) + ""; 
    if('NaN' == money){
    return  "0.00";
    }
    var money_flag = "";
    if(money.indexOf("-") != -1){
    money = money.replace(/-/g,"");
    money_flag = "-";
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgfjdz.html