JavaScript data type conversion

A, is converted to a string type

  1, toString () function

    Numerical, Boolean, objects, and string values (each string also has a toString () method, which returns a copy of the string) are toString () method.

    But null and undefined values do not have this method .

    In most cases, calls the toString () method does not have to pass parameters.
    Numerical call toString () time method, can pass a parameter: base output value.

    By default, toString () method returns the value represented by a string in decimal format. Passing through the base, toString () may output the string value in binary, octal, hexadecimal, as well as any other valid hexadecimal format
    Demo:

10 NUM = var; 
Alert (num.toString ()); // "10" in decimal default 
alert (num.toString (2)); // "1010" binary 
alert (num.toString (8)); // " 12 "octal 
alert (num.toString (10)); //" 10 " decimal 
alert (num.toString (16)); //" a " hex

  2, String () function

    We do not know the value of the conversion is not null or undefined in the case, may also be used transition function String () , this function can be converted to a string value of any type.

    Conversion rules:

      •  If the values are toString () method, this method is called (no parameters) and return the results;
      •     If the value is null , it returns "null" ;
      •     If the value is undefined , it returns "undefined" .

    Demo:

var value1 = null;
var value2;
console.log(String(value1));         // "null"
console.log(String(value2));         // "undefined"

  3, string concatenation method

    When both sides of the + operator is a string type, an operator when other types, will be first converted into a string and then other types of splicing string, it returns a string.

    Put a value into a string, the operator can use the plus it with an empty string (  "" ) can be added together.

Second, the converted value type

  1, Number () function

    This function can be used for any type of data.

    Conversion rules:

      •  If a Boolean value, to true and false , respectively is converted to 1 and 0 .
      •    If it is a numeric value, simply incoming and return.
      •    If a null value, returns 0 .
      •    If it is undefined , return NaN .
      •    If a string, follow these rules:
        •     如果字符串中只包含数字(包括前面带正号或负号的情况),则将其转换为十进制数值, "1"会变成 1"123"会变成 123,而"011"会变成 11(注意:前导的零被忽略了);
        •        如果字符串中包含有效的浮点格式,如"1.1",则将其转换为对应的浮点数值(同样,也会忽略前导零);
        •        如果字符串中包含有效的十六进制格式,例如 "0xf",则将其转换为相同大小的十进制整数值;
        •       如果字符串是空的(不包含任何字符),则将其转换为 0
        •       如果字符串中包含除上述格式之外的字符,则将其转换为 NaN
      •  如果是对象,则调用对象的 valueOf()方法,然后依照前面的规则转换返回的值。如果转换的结果是 NaN,则调用对象的 toString()方法,然后再次依照前面的规则转换返回的字符串值。

  2、parseInt() 函数

    该函数专门用于把字符串转换成整数。

    该函数会忽略字符串前面的空格,直至找到第一个非空格字符。

    如果第一个字符不是数字字符或者负号, parseInt()就会返回 NaNparseInt()转换空字符串会返回 NaNNumber()对空字符返回 0)。

    如果第一个字符是数字字符, parseInt()会继续解析第二个字符,直到解析完所有后续字符或者遇到了一个非数字字符。 (遇到小数点不会解析)
   扩展:
    如果字符串中的第一个字符是数字字符, parseInt()也能够识别出各种整数格式(即十进制、八进制和十六进制数)。

    如果字符串以"0x"开头且后跟数字字符,就会将其当作一个十六进制整数;如果字符串以"0"开头且后跟数字字符,则会将其当作一个八进制数来解析。
    但是这样来说并不严谨,对于八进制来说,前面的标识 “0” 可能会被认为无效,从而舍去。所以,可以为这个函数提供第二个参数:转换时使用的基数(即多少进制)。

    Demo:

var num1 = parseInt("10", 2);    //2 (按二进制解析)
var num2 = parseInt("10", 8);    //8 (按八进制解析)
var num3 = parseInt("10", 10);   //10 (按十进制解析)
var num4 = parseInt("10", 16);   //16 (按十六进制解析)

    Tips:多数情况下,我们要解析的都是十进制数值,因此始终将 10 作为第二个参数是非常必要的。

  3、parseFloat() 函数

    该函数专门用于把字符串转换成浮点数。

    该函数也是从第一个字符(位置 0)开始解析每个字符。而且也是一直解析到字符串末尾,或者解析到遇见一个无效的浮点数字字符为止。

    字符串中的第一个小数点是有效的,而第二个小数点就是无效的了,因此它后面的字符串将被忽略 。例:"22.34.5"将会被转换为 22.34
    与 parseInt() 区别:

      始终都会忽略前导的零。 由于 parseFloat()只解析十进制值,因此它没有用第二个参数指定基数的用法。
    Demo:

var num1 = parseFloat("1234blue"); //1234 (整数)
var num2 = parseFloat("0xA"); //0
var num3 = parseFloat("22.5"); //22.5
var num4 = parseFloat("22.34.5"); //22.34
var num5 = parseFloat("0908.5"); //908.5
var num6 = parseFloat("3.125e7"); //31250000

  4、“-0” 操作和 加 “+” 操作

    Demo:

var str = '500';
console.log(+str);		// 取正
console.log(-str);		// 取负
console.log(str - 0);

   分析:对于上面的一个字符串,可以观察到里面是由数字组成的。可以在字符串前面添加 “+” 或者 “-”号使之变成一个数值类型,也可以让字符串执行 “-0” 的操作。

三、转化成布尔类型

  1、Boolean() 函数

    用法

Boolean(参数);

     可以对任何数据类型的值调用 Boolean()函数,而且总会返回一个 Boolean 值。

    转换规则:

    0、‘’(空字符串)、NaN、null 和 undefined 都会转成 false,其他的都为 true。

  2、布尔类型的隐式转换 

    在 if 等流程控制语句中会把下面的值隐式转换成布尔类型:

    转换成 true:  非空字符串、非0数字, true ,任何对象

    转换成 false:  空字符串、0、false、null、undefined

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11298426.html