/*
 *公共方法 
 *时间格式
 *
 *
 */

let MoneyUtil = {
    accMul :(arg1,arg2) =>{
        if(arg1 && arg2){
            arg1 = Number(arg1)
            arg2 = Number(arg2)
            var m=0,s1=arg1.toString(),
            s2=arg2.toString();  
            try{
            m+=s1.split(".")[1].length}catch(e){}  
            try{
            m+=s2.split(".")[1].length}catch(e){}  
            return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m) 
        }else{
            return 0
        }
        
    },
    //arg1除以arg2的精确结果
    divide:(arg1,arg2)=>{
        if(arg1 && arg2){
            arg1 = Number(arg1)
            arg2 = Number(arg2)
            var t1=0,t2=0,r1,r2;
            try{t1=arg1.toString().split(".")[1].length}catch(e){};
            try{t2=arg2.toString().split(".")[1].length}catch(e){};
            
            r1=Number(arg1.toString().replace(".",""))
            r2=Number(arg2.toString().replace(".",""))
            return (r1/r2)*Math.pow(10,t2-t1);
        }else{
            return 0
        }
        
        
    },
    //获取 四舍五入 保留n位小数
    moneyFixed :(x,pos) =>{
        function accMul(arg1,arg2){
            arg1 = Number(arg1)
            arg2 = Number(arg2)
            var m=0,s1=arg1.toString(),
            s2=arg2.toString();  
            try{
            m+=s1.split(".")[1].length}catch(e){}  
            try{
            m+=s2.split(".")[1].length}catch(e){}  
            return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m) 
        }

        function divide(arg1,arg2){
            arg1 = Number(arg1)
            arg2 = Number(arg2)
            var t1=0,t2=0,r1,r2;
            try{t1=arg1.toString().split(".")[1].length}catch(e){};
            try{t2=arg2.toString().split(".")[1].length}catch(e){};
            
            r1=Number(arg1.toString().replace(".",""))
            r2=Number(arg2.toString().replace(".",""))
            return (r1/r2)*Math.pow(10,t2-t1);
        }
        var f = parseFloat(x);
        if(isNaN(f)){
            return false;
        }   

        f = divide(Math.round(accMul(x,Math.pow(10, pos))),Math.pow(10, pos)); // pow 幂   

        var s = f.toString();
        var rs = s.indexOf('.');
        if(rs < 0){
            rs = s.length;
            s += '.'; 
        }
        while(s.length <= rs + pos){
            s += '0';
        }
        return s;
    },
    //获取 不四舍五入 保留n位小数
    moneyNotFiexed:(num,decimal)=>{
        num = num.toString()
        let index = num.indexOf('.')
        if (index !== -1) {
            num = num.substring(0, decimal + index + 1)
        } else {
            num = num.substring(0)
        }
        return parseFloat(num).toFixed(decimal)
    }
};
let capitalizationAmount = {
moneyToCapital(num) {
    var fuhao = "";
    var text = num + "";
    if (text.indexOf("-") > -1) {
        num = text.replace("-", "");
        fuhao = "负"
    }
    var money1 = new Number(num);
    var monee = Math.round(money1 * 100).toString(10);
    var leng = monee.length;
    var monval = "";
    for (let i = 0; i < leng; i++) {
        monval = monval + this.to_upper(monee.charAt(i)) + this.to_mon(leng - i - 1)
    }
    return fuhao + this.repace_acc(monval)
},
// 将数字转为大写的中文字
to_upper(a) {
    switch (a) {
        case "0":
            return "零";
            break;
        case "1":
            return "壹";
            break;
        case "2":
            return "贰";
            break;
        case "3":
            return "叁";
            break;
        case "4":
            return "肆";
            break;
        case "5":
            return "伍";
            break;
        case "6":
            return "陆";
            break;
        case "7":
            return "柒";
            break;
        case "8":
            return "捌";
            break;
        case "9":
            return "玖";
            break;
        default:
            return ""
    }
},
to_mon(a) {
    if (a > 10) {
        a = a - 8;
        return (this.to_mon(a))
    }
    switch (a) {
        case 0:
            return "分";
            break;
        case 1:
            return "角";
            break;
        case 2:
            return "元";
            break;
        case 3:
            return "拾";
            break;
        case 4:
            return "佰";
            break;
        case 5:
            return "仟";
            break;
        case 6:
            return "万";
            break;
        case 7:
            return "拾";
            break;
        case 8:
            return "佰";
            break;
        case 9:
            return "仟";
            break;
        case 10:
            return "亿";
            break
    }
},
repace_acc(Money) {
    Money = Money.replace("零分", "");
    Money = Money.replace("零角", "零");
    var yy;
    var outmoney;
    outmoney = Money;
    yy = 0;
    while (true) {
        var lett = outmoney.length;
        outmoney = outmoney.replace("零元", "元");
        outmoney = outmoney.replace("零万", "万");
        outmoney = outmoney.replace("零亿", "亿");
        outmoney = outmoney.replace("零仟", "零");
        outmoney = outmoney.replace("零佰", "零");
        outmoney = outmoney.replace("零零", "零");
        outmoney = outmoney.replace("零拾", "零");
        outmoney = outmoney.replace("亿万", "亿零");
        outmoney = outmoney.replace("万仟", "万零");
        outmoney = outmoney.replace("仟佰", "仟零");
        yy = outmoney.length;
        if (yy == lett) {
            break
        }
    }
    yy = outmoney.length;
    if (outmoney.charAt(yy - 1) == "零") {
        outmoney = outmoney.substring(0, yy - 1)
    }
    yy = outmoney.length;
    if (outmoney.charAt(yy - 1) == "元") {
        outmoney = outmoney + "整"
    }
    return outmoney
}
};
export {
MoneyUtil,
capitalizationAmount
};