JavaScript serialization 5-data conversion to Number and String, Number parsing

Keywords: Javascript github Java Big Data

1, Series 4

3. If null and undefined do not have toString() method, the call will report an error

    var num1 = undefined;

    console.log(num1.toString());

​

    var num2 = null;

    console.log(num2.toString());

2, String()

1. Regular use

Some values do not have the toString() method. You can use the String() method at this time, such as: null and undefined

2. Use notes

(1) null and undefined, instead of calling toString() method, directly convert to string

(2) For data of type Number and Boolean, 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. How to splice strings

(1) General usage: any data + "" connected together will be converted to a string; 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

(1) String to number

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

(2) Boolean to number

true to 1, false to 0;

(3) null and undefined converted to numbers

null to 0, undefined 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) Usage meaning: no matter whether there is a valid integer in the string, the Number function will directly return NaN. The parseInt() and parseFloat() functions can be used to extract the valid integer and floating-point Number in the string

(2) parseInt: there are two parameters. The first parameter is the string to be converted, and the second parameter is the base to be converted. It starts from the first significant number to the invalid number. If the first rage is a significant number, NaN is returned directly.

(3) parseFloat: the second parameter is not supported, and only decimal digits can be parsed; if the function in the parsed content contains integers, only integers will be parsed; if the first digit is not a valid digit, NaN will be returned directly; starting from a valid digit 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 numbers with units, such as 15 px such
var a3 = "4554.154dsaf";

    var a4 = "josf2.35";

    var a5 = "45895dhaif";

    console.log(parseFloat(a3));

    console.log(typeof parseFloat(a3));

    console.log(parseFloat(a4));

    console.log(typeof parseFloat(a4));

    console.log(parseFloat(a5));

    console.log(typeof parseFloat(a5));

3, 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/ruige0000/

4. welcome to WeChat official account: Fourier transform, personal account, only for technology exchange, backstage reply "gift package" to get Java big data learning video package.

 

Posted by wellscam on Sun, 19 Apr 2020 04:45:09 -0700