Three ways to convert strings to numbers using JS

There are three main methods:
conversion function, forced type conversion, and weak type conversion using js variables.

1. Conversion function:

js provides parseInt()and parseFloat()two conversion functions. The former converts the value to an integer, the latter converts the value to a floating point number. These two functions work correctly only if these methods are called on the String type; all other types return NaN(Not a Number).
Some examples are as follows:

parseInt("1234blue");   //returns   1234  
parseInt("0xA");   //returns   10  
parseInt("22.5");   //returns   22  
parseInt("blue");   //returns   NaN  

parseInt()Methods also have base modes, which can convert binary, octal, hexadecimal, or any other base string to an integer. The base is parseInt()specified by the second parameter of the method, an example is as follows:

parseInt("AF", 16);   //returns   175  
parseInt("10", 2);   //returns   2  
parseInt("10", 8);   //returns   8  
parseInt("10", 10);   //returns   10  

If the decimal number contains leading 0s, it is best to use base 10 so that you don't accidentally get an octal value. E.g:

parseInt("010");   //returns   8  
parseInt("010", 8);   //returns   8  
parseInt("010", 10);   //returns   10  

parseFloat()Methods parseInt()are handled similarly to methods.
Another difference in usage parseFloat()is that strings must represent floating point numbers in decimal, with parseFloat()no base mode.
Here is parseFloat()an example of how to use it:

parseFloat("1234blue");   //returns   1234.0  
parseFloat("0xA");   //returns   NaN  
parseFloat("22.5");   //returns   22.5  
parseFloat("22.34.5");   //returns   22.34  
parseFloat("0908");   //returns   908  
parseFloat("blue");   //returns   NaN  

2. Forced type conversion

The type of the converted value can also be handled using type casting. Use casts to access a specific value even if it is of another type.
The three types of casts available in ECMAScript are as follows:
Boolean(value)- Converts the given value to a Boolean;
Number(value)- Converts the given value to a number (can be an integer or a floating-point number);
String(value)- Converts the given value into a string.
Converting a value with one of these three functions creates a new value that holds the value directly converted from the original value. This can have unintended consequences.

The Boolean() function returns true when the value to be converted is a string with at least one character, a non-zero number, or an object (discussed in the next section). It will return false if the value is an empty string, the number 0, undefined or null.

Boolean casts can be tested with the following code snippet.

Boolean("");   //false   –   empty   string  
Boolean("hi");   //true   –   non-empty   string  
Boolean(100);   //true   –   non-zero   number  
Boolean(null);   //false   -   null  
Boolean(0);   //false   -   zero  
Boolean(new Object());   //true   –   object  

Number()The coercion of the parseInt()and parseFloat()method is handled in a similar way, except that it converts the entire value instead of part of it. An example is as follows:

用  法                   结  果  
Number(false)                  0  
Number(true)                   1  
Number(undefined)              NaN  
Number(null)                   0  
Number( "5.5 ")                5.5  
Number( "56 ")                 56  
Number( "5.6.7 ")              NaN  
Number(new   Object())         NaN  
Number(100)                    100  

The last method of coercion String()is the simplest, and the example is as follows:

var s1 = String(null);   //"null"  
var oNull = null;  
var s2 = oNull.toString();   //won't work, causes an error  

3. Use js variable weak type conversion

Take a small example, and you will understand at a glance.

<script>  
var str= '012.345 ';  
var x = str-0;  
x = x*1;  
</script>  

4. Using some symbols can also do type conversion

    X + '' // 等价于 String(X)
    +X    //  等价于 Number(X),也可以写成 x-0
    !!X   // 等价于 Boolean(X)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568894&siteId=291194637