JS to convert numbers into comma separated formatted strings

Time:2020-10-29

 

//Note: the conversion of numbers to characters will lose the extra trailing 0 ('123.010'123.01, '123.00'123)
var NumberFormat = {
    //According to every 3-bit comma separation, string truncated string method
    toCommaFormat: function(number){
        //If it is a number, it is converted to a string
        if(typeof number == 'number'){
            number = String(number)
        }
        
        var pointIndex =  number.lastIndexOf ('); // gets the position of the decimal point
        Var hasdecimal = pointindex! = - 1; // check for decimal points
        var numberInteger = hasDecimal ?  number.substring (0, pointindex): number; // gets the integer part of a number
        var length = numberInteger.length;
        if(length > 3){
            var numberFormat = '';
            //Cut the string from the tail to the front
            for(var i= numberInteger.length; i> 0; i=i-3){
                //Current substring + previously spliced substring (because: substring (0,1) = = substring (- 2,1), there is no need to judge if I-3 is negative)
                numberFormat = numberInteger.substring(i-3,i) + (numberFormat==''?'':(','+numberFormat));
            }
            //Integral part + decimal part
            var numberDecimal = hasDecimal ? number.substring(pointIndex) : '';
            return numberFormat + numberDecimal;
        }else{
            //No need to separate, return directly
            return number;
        }
    },
    //According to every 3-digit comma separation, the remainder splicing method (disadvantage: limited number of digits)
    toCommaFormat2: function(number){
        //If it is a number, it is converted to a string
        if(typeof number == 'number'){
            number = String(number)
        }
        
        var pointIndex =  number.lastIndexOf ('); // gets the position of the decimal point
        Var hasdecimal = pointindex! = - 1; // check for decimal points
        var numberInteger = hasDecimal ?  number.substring (0, pointindex): number; // gets the integer part of a number
        var length = numberInteger.length;
        if(length > 3){
            var numberFormat = '';
            Var remainder; // remainder
            Var quotient; // quotient
            while(true){ 
                quotient =  Math.floor (number (numberinteger) / 1000); // do you need to further separate the trademarks
                Remainder = number (numberinteger)% 1000; // the remainder is the separated substring
                Numberformat = string (remainder) + (numberformat = = '?'): (',' + numberformat)); // the remainder before the current remainder is concatenated
                if(quotient == 0){
                    //If the quotient is 0, the calculation is completed
                    break;
                }
                //The quotient, as the divisor, is further separated
                numberInteger =  quotient; 
            }
            //Integral part + decimal part
            var numberDecimal = hasDecimal ? number.substring(pointIndex) : '';
            return numberFormat + numberDecimal;
        }else{
            //No need to separate, return directly
            return number;
        }
        
    },
    //Regular replacement, from netizens
    format_number:function(nStr ){
        Nstr + = '"; // number converted to character 
        x =  nStr.split ('); // separated by decimal point
        X1 = x [0]; // integer part
        X2 = x.length > 1? '.' + x [1]: "; // decimal part
        Var RgX = / (- D +) (\ D {3}) /; // regular definition
        while ( rgx.test (x1)) {// regular match
            X1 = x1. Replace (RgX, '$1' + ',' + '$2'); // regular substitution
        }  
        return x1 + x2;  
    } 
    
};

 

Summary:

1. It is recommended to use the method of string truncation to format, so there is no length limit.

2. It is better to use the string type to pass the parameters by calling the function, so that the trailing 0 will not be lost.

3. Netizens’ regularization method is very simple and beautiful, as well as string separation into single character reverse order splicing method.