There are three functions for converting non-numerical values into numerical values: Number (), parseInt (), and parseFloat ().

Note tip: Number() can be used for any data type; parseInt(),parseFloat() is for strings;

Number() conversion rules are as follows: give some special examples;

The first category: null, undefined

 console.log(Number(null)) //The return is 0.
 console.log(Number(undefined)) //NaN is returned;

Category 2: Strings

  /*************If it's a string ****************/
        console.log(Number('-123')) //Returns a - 123 // string that contains only digits (including a plus and minus sign for the first character of the string), converts it to decimal digits, and is ignored if the first character is zero.
        console.log(Number('+123'))  //Return to 123
        console.log(Number('123'))  //Return to 123
        console.log(Number('0123'))  //Return to 123

        //If the string contains a valid floating-point format, the corresponding conversion to the corresponding floating-point number, similar to the above rule, the first character of o is ignored.
        console.log(Number('01.2')) //Return to 1.2
        console.log(Number('1.2')) //Return to 1.2

        //If the string contains a valid hexadecimal number, it is converted to a decimal number of the same size.
        console.log(Number('0xaf')) //Return to 175
        //If the string is empty, 0 is returned.
        console.log(Number('')) //0

        //In addition to the above format, NaN is returned.
        console.log(Number('0120sd'))  //NaN

Category III: object According to javascript advanced programming (third edition), if it is an object, it calls the valueOf() method of the object, and then converts the returned value according to the previous rules. If the result of the conversion is NaN (non-numeric), it calls the toString() method of the object, and then converts the returned value according to the previous rules.

        var ary_=new Array('20',25);
        console.log(ary_.valueOf());//['20', 25]; instance objects of arrays, non-numeric
        console.log(ary_.toString())//20,25 strings
        console.log(Number(ary_))  //NaN

        var boolean=new Boolean(85)
        console.log(boolean.toString())//String true

        var date=new Date(2018,3,18)
        console.log(date.valueOf())  //1523980800000
        console.log(date.toString())  //Wed Apr 18 2018 00:00 GMT+0800 (China Standard Time)
        console.log(Number(date))  //1523980800000

        function test(){
        console.log(test.valueOf())//Return function itself, non-numeric
        console.log(test.toString())//String form of return function

        var num=new Number(52);
        console.log(num.valueOf())//Numerical value 52
        console.log(Number(num))//Numerical value 52

        var obj={
        console.log(obj.valueOf());//Return itself, non-numeric
        console.log(obj.toString());//[object Object object] string
        console.log(Number(obj))  //NaN

        var str=new String('sdsf52');
        console.log(str.valueOf())//'sdsf52', the value of the string

parseInt() transformation rule

tip:1: Ignore the space in front of the string. From the first character, if the first string is not a numeric character or a minus sign, the function returns NaN and resolves to the last character. If a non-numeric character is encountered halfway, it stops, such as parseInt('2025zh526'), and 2025 is returned.

tip2:parseInt() recognizes the format of integers, decimal or octal or hexadecimal; starts with 0x, followed by a number of Hexadecimal; octal at the beginning of 0 followed by a number; but under the ECMAScript 5js engine, parseInt() does not have the ability to parse octal, such as parseInt('050'); returns 50;

tip3: To eliminate the confusion mentioned above, parseInt() provides a second parameter to determine which decimal string to parse, as shown in the following example

console.log(parseInt('ad'))   //NaN
console.log(parseInt('ad',16)) //173
console.log(parseInt('10',8))  //8

parseFloat() transformation rule

tip1:parseFloat() only parses decimal values;

tip2: Ignore the space in front of the string, parse from the first character (if the first character is zero, it will be ignored) to the end of the string until it is parsed to an invalid floating-point character. The first decimal point of the string is valid, the second is invalid, and the character after the second decimal point will be ignored. Here are some special examples.

tip3: A string containing an integer, or 0 after a decimal point, returns an integer


Keywords: Javascript Programming ECMAScript

Added by AngelGSD on Tue, 14 May 2019 19:54:41 +0300