About the difference between toString() and toLocaleString()

Introduce

Today, when discussing with your little buddy how to convert a number num=123123123 to the format 123,123,123 corresponding to the amount, you learned two ideas. First, write down:

The code I wrote is like this: (Writing the function to death is frustrating, only the current requirements can be met,,)

function addD(num) {
    var sp = num.toString().split("");
    for (var i = 0; i < sp.length; i++) {
        if (sp[i]== '3') {
            sp.splice(++i,0,',');
        }
    }
    ss = sp.join("");
    ss = ss.substring(0,ss.length-1);
    return ss;
}

var ss = addD(123123123);
console.log(ss);

The first thought you learned is to insert a comma every 3 places, moving back and forth.

function money(num) {  
    var str = num.toString().split(''); // Converts a number to a single character array
    const length = str.length;
    for (let i = length-1; i >= 1; i--) {  // Insert a comma every 3 digits from back to front
        if ((length - i) % 3 == 0) {
            str.splice(i, 0, ',');
        }
    }
    str = str.join('');
    return str;
}
console.log(money(123123123));

Second thought:

function toMoney(num) {
    num = num.toFixed(2); //Converting a number to a string with 2 decimal places, the banker's algorithm, rounding in half, has a minor problem. Chrome does not follow. This means precise to 2, look at the third place, the fifth place is 5, the number after 5 goes forward 1, without directly rounding it, the best use of the amount involved.
    num = parseFloat(num); //Converts a string with a 2-digit decimal number to a number with a decimal number
    num = num.toLocaleString(); //Convert numbers with 2 decimal places into amount format
    return num; //Returns a string of 23,245.12 reserving 2 decimal places
}

let num1 = 5262456.2158;
let num1 = 5262456.215;
console.log(toMoney(num1));  // 5,262,456.22
console.log(toMoney(num2)); // 5,262,456.21

In this code, it is found that num.toLocaleString() in the toMoney function can directly convert the number of two decimal digits into the format of the amount, which is not the requirement of the above topic.Usually, toString() is the only method used, but toLocaleString () is rarely used.What is the difference between the two?Go to MDN.

The difference between toString() and toLocaleString()

  1. Interpretation of MDN:

    1. The toString() method returns the string representation of the specified Number object
      • Syntax: numObj.toString([radix])
      • Parameter: Radix specifies the cardinality (from 2 to 36) to be used for the number-to-string conversion.If the radix parameter is not specified, the default value is 10.If the radix parameter of toString() is not between 2 and 36, a RangeError will be thrown.
    2. The toLocaleString() method returns the representation string of this number in a specific language environment
      • Syntax: See the exact meaning of the numObj.toLocaleString([locales [, options]]) parameter MDN.
      • When no parameters are specified: Returns a string formatted with the default locale and default options when no region is specified for basic use.For example:
        var number = 3500;
        console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale
        
  2. Specific cases of digit to string
    Integers less than 3 digits:

    var a = 300;
    
    console.log(a.toString());  // "300"
    console.log(a.toLocaleString());  //"300" makes no difference 
    

    Decimals less than 3 digits:

    var a = 3.01;
    
    console.log(a.toString());  // "3.01"
    console.log(a.toLocaleString());  //There is no difference between "3.01" 
    

    More than 3-bit integer

    var a = 300000;
    
    console.log(a.toString());  // "300000"
    console.log(a.toLocaleString());  //"300,000" every 3 digits 
    

    More than 3 decimal places

    var a = 300000.3454;
    
    console.log(a.toString());  // "300000.3454"
    console.log(a.toLocaleString());  //"300,000.345"  
    
    var a = 300000.3455;
    
    console.log(a.toString());  // "300000.3455"
    console.log(a.toLocaleString());  //"300,000.346" rounds off three decimal places, and integer parts are separated by commas every three digits.
    
    var a = 300000.3455321;
    
    console.log(a.toString());  // "300000.3455321"
    console.log(a.toLocaleString());  //"300,000.346" rounds off three decimal places, and integer parts are separated by commas every three digits.
    
  3. Differences in conversion time formats

    var d = new Date()
    console.log(d);  // 2020-03-13T01:27:13.465Z
    console.log(d.toString()); // Fri Mar 13 2020 09:27:13 GMT+0800 (China Standard Time)
    console.log(d.toLocaleString()); // 2020/3/13 9:27:13 AM
    

summary

  1. You learned two ideas for converting numbers to amounts: inserting commas backwards and forwards; and using toLocalString().
  2. Two main differences between toString() and toLocaleString(): numerically and in time format, see the analysis above.
Forty-seven original articles were published, 13 were praised, and 3832 were visited
Private letter follow

Tags: less

Posted on Thu, 12 Mar 2020 22:04:03 -0400 by inztinkt