Angularjs指令格式化数字

时间:2016-04-18 11:25:34

标签: angularjs angularjs-directive numberformatter

我需要一个angularjs指令绑定到具有以下功能的输入框:

  • 以#,###
  • 的形式格式化十进制
  • 限制小数位数
  • 限制输入的数字

如果我想将十进制数字限制为2,输入的数字为8,则输出必须为:45,345,345.34

指令:

App.directive('amountInput',function($filter) {
    return {
        restrict : 'AE',
        scope : {
            ngModel : '=ngModel',
            limit:'=limit',
            declimit:'=declimit',
        },
        link : function(scope, elem, attr, ctrl) {
            scope.validateamount = function(e) {
                var theEvent = e || window.event;
                var key = theEvent.keyCode || theEvent.which;
                if (theEvent.altKey || theEvent.ctrlKey ){
                    return;
                 }
                if(theEvent.ctrlKey && key == 65 ){
                     return ;
                 }
                if(key == 16 || key == 36 || key == 37 || key == 38 || key == 39 || key == 40 ) { // Left / Up / Right / Down Arrow, Backspace, Delete keys
                    return;
                }
                // Regular
                var acceptChar = /^[|m|k|M|K|0-9,.]+$/gi;
                var acceptNumDotComma = /^[0-9.,]+$/
                var isCharHaveMnK = /\m|k/gi;
                var enteredString = $("#"+scope.amountId).val();
                var decimalString ;
                var preCommaCount = enteredString.substring(0,cursorPostion).split(",").length-1;
                enteredString = enteredString.replace(/,/g,"");
                var lastChar = enteredString.charAt(enteredString.length-1);
                if(lastChar.match(acceptChar)==null){
                    enteredString = enteredString.slice(0,-1)
                }
                if(enteredString.match(new RegExp('\\.','g'))!=null &&
                    enteredString.match(new RegExp('\\.','g')).length > 1){

                    var temp  = enteredString;
                    enteredString = temp.substring(0, temp.indexOf("."));
                    decimalString =  temp.substring(temp.indexOf(".")+1,temp.length);;
                    var Re = new RegExp("\\.","g");
                    decimalString = decimalString.replace(Re," ");
                    enteredString = enteredString+'.'+decimalString.trim();
                }
                if(enteredString.match(isCharHaveMnK) != null){

                    enteredString = enteredString.substring(0,enteredString.length-1);
                    var amount ;
                    if (lastChar == "m" || lastChar == "M") {
                        amount = (enteredString * 1000000);
                    } else if (lastChar == "k" || lastChar == "K") {
                        amount = (enteredString * 1000);
                    }   
                    amount = Math.round(amount)
                    amount = amount+""; 
                    cursorPostion = amount.length;
                    if(amount.length > scope.limit){
                        enteredString = enteredString.substring(0,scope.limit);
                    }else{
                        enteredString = amount;
                    }
                }

                //Dont accept char above the limit
                decimalString = "";
                if(enteredString.indexOf(".")!=-1){
                    var temp  = enteredString;
                    enteredString = temp.substring(0, temp.indexOf("."));
                    decimalString =  temp.substring(temp.indexOf("."),temp.length);;
                }

                if(enteredString.length > scope.limit){
                    enteredString = enteredString.substring(0,scope.limit);
                }


                //Remove Spl char from Decimal point
                var res = decimalString.split("");
                decimalString  = "";
                for(var i = 0 ; i < res.length;i++){
                    if(res[i] == "."){
                        decimalString = decimalString+res[i]; 
                    }else if(!isNaN(res[i]) && res[i]!=' ' ){
                        decimalString = decimalString+res[i];
                    } 
                }

                if(scope.declimit==0){
                    decimalString="";
                }else if(decimalString.length-1 > scope.declimit){
                    decimalString = decimalString.substring(0,parseInt(scope.declimit)+1);
                }

                res = enteredString.split("");
                enteredString  = "";
                for(var i = 0 ; i < res.length;i++){
                    if(!isNaN(res[i]) && res[i]!=' '){
                        enteredString = enteredString+res[i];
                    } 
                }
                var temp1 = scope.numberformatter(enteredString) +decimalString;         

                var prefixCommaCount = temp1.substring(0,cursorPostion).split(",").length-1;
                var count= prefixCommaCount-preCommaCount ;
                var suffixChar = temp1.substring(parseInt(cursorPostion)-count,temp1.length);
            scope.numberformatter = function(obj){
                if(obj!='0'){
                    return $.formatNumber(obj, {format:"#,###", locale:"us"});
                }
                return obj;
            };

        },
    };
});

0 个答案:

没有答案