js in the parseInt () and parseFloat (), Number (), Boolean (), String () Conversion

The transfer method of the string value js three main

 

Transfer function, casts, use of weakly js variable type conversion.

 

 

1. conversion functions:

 

js Providing the parseInt () and parseFloat () two conversion functions. The former value into an integer, which converts into a floating point value. Only call these methods String type, these two functions to run correctly; for other types of returns are NaN (Not a Number).

 

In determining whether the value of the string before the number is, parseInt () and parseFloat () will carefully analyze the string.

 

the parseInt () method first check character position 0, it is determined whether or not it is a valid number; if not, the method returns NaN, does not continue to perform other operations. But if the character is a valid number, which will look at the character in the position 1, the same test. This process continues until it reaches the non-active character figures, at this time the parseInt () will convert the character string into digital before.

 

For example, if we want the string "1234blue" to an integer, then the parseInt () returns 1234, when it is detected as the character B, the detection process will stop.

 

Literal numeric string is contained correctly converted to digital, so the string "0xA" is converted into a digital 10 correctly.

 

However, the string "22.5" will be converted to 22, as for the integer, the decimal point is an invalid character.

 

Some examples are as follows:

 

Js Code
  1. parseInt("1234blue");   //returns    1234   
  2. parseInt("0xA");   //returns    10   
  3. parseInt("22.5");   //returns    22   
  4. parseInt("blue");   //returns    NaN  

 

the parseInt () method also group mode, can be converted to binary, octal, hexadecimal, binary string, or any other integer.

 

Group is specified by the second parameter the parseInt () method, so to resolve the hexadecimal value, to be called the parseInt () method is as follows:

Js Code
  1. parseInt("AF",    16);   //returns    175   


Of course, binary, octal, decimal or even (the default mode), you can call this parseInt () method:

 

Js Code
  1. parseInt("10",    2);   //returns    2   
  2. parseInt("10",    8);   //returns    8   
  3. parseInt("10",    10);   //returns    10   


If the decimal number contains leading 0, then the best use of the base 10, so as not to accidentally got octal values. E.g:

 

Js Code
  1. parseInt("010");   //returns    8   
  2. parseInt("010",    8);   //returns    8   
  3. parseInt("010",    10);   //returns    10   


In this code, two lines of code Strings are "010" Analysis has become a number. The first line of code to the string as octal values, and the way it resolved second line (declared cardinality. 8). The last line of code declares base 10, so iNum3 equal to the last 10.

 

Similar treatment parseFloat () method with the parseInt () method, view each character starts from the position 0, until you find the first non-active character, and the character string before conversion to digital.

 

However, for this method, the first occurrence of a decimal point is a valid character. If there are two decimal points, a second decimal point will be considered invalid, parseFloat () method of the string before the decimal point will convert to digital. This means that the string "22.34.5" will be resolved to 22.34.


Use parseFloat () method is another difference is that the string must be expressed in decimal floating-point form, rather than using octal or hexadecimal.

 

This method ignores the leading 0, so the octal number 0908 is interpreted as 908. For the hexadecimal number 0xA, the method returns NaN, because the float, x is not a valid character.

 

Further, parseFloat () nor group mode.

 

The following are examples of using parseFloat () method:

Js Code
  1. parseFloat("1234blue");   //returns    1234.0   
  2. parseFloat("0xA");   //returns    NaN   
  3. parseFloat("22.5");   //returns    22.5   
  4. parseFloat("22.34.5");   //returns    22.34   
  5. parseFloat("0908");   //returns    908   
  6. parseFloat("blue");   //returns    NaN  

 

 

 

2. cast

 

Use may also be cast (type casting) process type conversion value. Use Casts may access a specific value, even if it is another type.


ECMAScript can be used in three types of cast follows:


Boolean (value) - the given values into a Boolean; 
Number The (value) - the given value into a digital (either integer or floating point); 
String (value) - the value given into a string.


A conversion value of one of these three functions, will create a new value, for translation from the original value directly into the value. This will result in unintended consequences.

 

When the value is converted to at least one character string, or an object other than 0 (this is discussed in the next section) is, Boolean () function returns true. If the value is an empty string, number 0, undefined, or null, it returns false.

 

Code segment may be used to test the following Boolean type casts.

 

Js Code
  1. Boolean("");   //false    –    empty    string   
  2. Boolean("hi");   //true    –    non-empty    string   
  3. Boolean(100);   //true    –    non-zero    number   
  4. Boolean(null);   //false    -    null   
  5. Boolean(0);   //false    -    zero   
  6. Boolean(new    Object());   //true    –    object  

Number () is cast with the parseInt () and parseFloat () method similar treatment, except that it converts the entire value, not partial values.

 

Remember, parseInt () and parseFloat () method only convert a string before the first invalid character, so "4.5.6" will be converted to "4.5."

 

A cast with Number (), "4.5.6" would return NaN, because the entire character string can not be converted into a digital value.

 

If the string value can be completely converted, Number () call to the determination is the parseInt () method is invoked parseFloat () method.

 

The following table illustrates the situation calls Number of different values ​​() method will happen:

 

Use results

Js Code
  1. Number(false)    0   
  2. Number(true)    1   
  3. Number(undefined)    NaN   
  4. Number(null)    0   
  5. Number( "5.5 ")    5.5   
  6. Number( "56 ")    56   
  7. Number( "5.6.7 ")    NaN   
  8. Number(new    Object())    NaN   
  9. Number(100)    100    

The final cast method String () is the simplest, because it can convert any value into a string.

 

To perform this cast, simply call toString value passed in as a parameter () method, that is converted into a "1", the true converted to "true", the false convert to "false", so analogy.

 

Coerced into character strings and call toString () method only difference that the value of null or undefined cast string can be generated without raising error:

 

Js Code
  1. var    s1    =    String(null);   //"null"   
  2. was oNull =    null;   
  3. var    s2    =    oNull.toString();   //won’t    work,    causes    an    error  

 

 

 

3. The use of a weakly typed variable conversion js

 

Give a small example, a look, you will understand.

Js Code
  1. <script>   
  2.     was str =  '012345';   
  3.     where x = 0 str;   
  4.      x    =    x*1;   
  5. </script>  

Js embodiment utilizes the weak type characteristics, only the arithmetic operations, to achieve a string type digital conversion, but this method is not recommended.

Guess you like

Origin www.cnblogs.com/qhantime/p/11324115.html