Thousands of digit format of comma separated code has been modified to support 0 9 comma separated JS code

  • 2020-03-30 00:43:37
  • OfStack

Recent work on the project required us to format the amount in thousandths (that is, separated by commas for every three digits) in the front end, and the code has been modified   The previous version is my negligence really sorry for everyone! Now has made the modification if there is not perfect place please advise!

JS code is as follows:




         function numFormat(number,d) {

             var numArrs = ['0','1','2','3','4','5','6','7','8','9'],
                 REG_NUMBER = /^d+(.d+)?$/;

             d = d || 3; //It's three thousandths
             if(isNumber(number) || isString(number) || REG_NUMBER.test(number)) {
                 //I'm going to convert it to a string
                 var toString = number + '',
                     isPoint = toString.indexOf('.'),
                     prefix,   //  The prefix 
                     suffix,   //  The suffix 
                     t = '';

                 if(isPoint > 0) {
                    prefix = toString.substring(0,isPoint);
                    suffix = toString.substring(isPoint + 1);
                 }else if(isPoint == 0) {
                    prefix = '';
                    suffix = toString.substring(1);
                 }else {
                    prefix = toString;
                    suffix = '';
                 }

                 if(prefix != '') {
                    prefixArr = prefix.split('').reverse();
                    var isArrayIndex = isArray(d,numArrs);
                    if(isArrayIndex > -1) {
                        for(var i = 0, ilen = prefixArr.length; i < ilen; i+=1) {
                            t += prefixArr[i] + ((i + 1) % isArrayIndex == 0 && (i + 1) != prefixArr.length ? "," : "");
                        }
                        t = t.split("").reverse().join("");
                        if(suffix != '') {
                            return t + "." + suffix;
                        }else {
                            return t;
                        }

                    }else {
                        return ' Incorrect number of bits passed in ';
                    }

                 }else if(prefix != '' && suffix == ''){
                    return prefix;
                 }else if(prefix == '' && suffix != ''){
                    prefix = 0;
                    return prefix + suffix;
                 }else {
                     return " There is an error ";
                 }
            }else {
                return ' The number passed in to format does not match ';
            }

         }
         function isArray(item,arrs) {
            for(var i = 0, ilen = arrs.length; i < ilen; i++) {
                if(item == arrs[i]) {
                    return i;
                }
            }
            return -1;
         }
         function isNumber(number) {
            return Object.prototype.toString.apply(number) === '[object Number]';
         }
         function isString(number) {
            return Object.prototype.toString.apply(number) === ['object String'];
         }

But what seems to be missing is the call to console.log(numFormat("1111.00"));   Output 1,111 directly from the console instead of 1,111.00 which means that if the decimal point is behind the zero the browser will automatically erase the zero and everything else will be fine! I have tested the basic requirements if there is not perfect place please advise!


Related articles: