JavaScript serialization 5-data conversion to Number and String, number analysis

One, undertake serialization 4

3. There is no toString () method for null and undefined, the call will report an error

    var num1 = undefined;

    console.log(num1.toString());

​

    var num2 = null;

    console.log(num2.toString());

Second, String ()

1. Regular use

Some values ​​do not have toString () method, at this time you can use String () method, for example: null and undefined

2. Attention

(1) null and undefined, the toString () method will not be called, but converted directly into a string

(2) For Number and Boolean data, the String () method is equivalent to calling the toString () method.

 

    var v1 = null;

    var v2 = String(v1);

    console.log(v2);

    console.log(typeof v2);

​

    var v3 = 20;

    var v4 = String(v3);

    console.log(v4);

3. Ways to stitch strings

(1) Conventional usage: any data + "" will be converted into a string when connected together; the internal implementation is the same as String ()

 

 

   var v5 = 1000;

    var v6 = v5+"";

    console.log(v6);

    console.log(typeof v6);

4. Convert other types to Number types

(1) String to number

If it is a pure number, it is directly converted to a number; if the string is empty or a string of all spaces, it is converted to 0; if there is non-numeric content in the string, it is converted to NaN;

(2) Boolean type conversion to numbers

true becomes 1 and false becomes 0;

(3) Null and undefined are converted into numbers

null is converted to 0, undefined is converted to NaN

 

    var v7 = "1015";

    var v8 = Number(v7);

    console.log(v7);

    console.log(typeof v8);

​

    var v9 = "";

    var v10 = Number(v9);

    console.log(v10);

    console.log(typeof v10);

​

    var v11 = "411jsdfo";

    var v12 = Number(v11);

    console.log(v12);

    console.log(typeof v12);

​

    var v13 = true;

    var v14 = Number(v13);

    console.log(v14);

    var v15 = false;

    var v16 = Number(v15);

    console.log(typeof v16);

​

    var v17 = null;

    var v18 = undefined;

    var v19 = Number(v17);

    var v20 = Number(v18);

    console.log(v19);

    console.log(v20);

5.parseInt and parseFloat functions

(1) Significance of use: No matter whether there is a valid integer in the string in the Number function, NaN will be returned directly. Using the parseInt () and parseFloat () functions, the valid integer and floating-point number in the string can be extracted

(2) parseInt: There are two parameters, the first parameter is the string to be converted, and the second parameter is the hexadecimal to be converted; from the first valid digit to the invalid digit; if the first digit is furious If it is a valid number, NaN is returned directly.

(3) parseFloat: The second parameter is not supported, only decimal numbers can be parsed; if the function contains an integer in the parsed content, it will only be parsed as an integer; if the first digit is not a valid number, NaN will be returned directly Start with significant digits until an invalid digit is encountered

 

    var a1 = "45145dsf45" ; 

    var a2 = "shofa45sdf" ; 

    console.log (parseInt (a1, 10 )); 

    console.log (parseInt (a2, 10 )); 

    // mainly used to extract number such that 15px 
var A3 = "4554.154dsaf" ; var A4 = "josf2.35" ; var A5 = "45895dhaif" ; 
    the console.log (parseFloat (A3)); 
    the console.log ( typeof parseFloat (A3) ); 
    console.log (parseFloat (a4)); 
    console.log ( typeof parseFloat (a4)); 
    console.log (parseFloat (a5)); 
    console.log (

    

    

    





typeof parseFloat(a5));

Third, the source code:

D5_1_DataTransfrom.html

address:

https://github.com/ruigege66/JavaScript/blob/master/D5_1_DataTransfrom.html

2.CSDN:https://blog.csdn.net/weixin_44630050

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. Welcome to pay attention to WeChat public account: Fourier transform, personal account, only for technical communication, reply "gift pack" in the background to get Java big data learning video pack

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/12732923.html