Js string into a number and a number into a string implementation method

  • 2020-03-30 01:14:53
  • OfStack

Js string to a number

To convert a string to a number parseInt Function.
ParseInt (string) : the function parses from the beginning of a string and returns an integer.


For example:
ParseInt ('123') : returns 123 (int);
ParseInt ('1234xxx') : returns 1234 (int);

If the number is not resolved, a value of NaN is returned, which can be detected using the isNaN() function.

For example:
Var I = parseInt (' ABC ');
If (isNaN (I))
{
Alert (' NaN value);
}

The same parseFloat function converts a string to a floating-point number.

Example: parseFloat('31.24 ABC ') : returns 31.24;


Js Numbers are converted to strings


To convert a string to a number The String Of the class The toString methods

For example:
Var I = 10;
Var s = i.t oString ();
Alert (typeof s); // will output a String


The difference between js Numbers and strings

Js number of the addition and string connection are + symbol, so whether to add or string connection depends on the type of the variable.

For example:
Var a = 'ABC' + 'xyz'; //a: abcxyz, string to string is the connection
So var a is equal to 10 plus 5; The value of //a is: 15, and the number is plus
Var a = 'ABC' + 10; The value of //a is: abc10, string and number, automatically converts 10 to a string
Var a = 'ABC' + 10 + 20 + 'CD '; //a is abc1020cd
Var a = 10 + 20 + 'ABC' + 'CD '; The value of //a is :30abccd, which can be added numerically first, and then connected

Supplement:

Js string converts Numbers. There are three main methods

Convert function, cast, use js variable weak type conversion.

1. Conversion function:

Js provides two conversion functions, parseInt() and parseFloat(). The former converts values to integers, and the latter converts values to floating point Numbers. Only by calling these methods on the String type can these two functions work correctly; NaN(Not a Number) is returned for all other types.

Some examples are:

ParseInt (1234 "blue"); / / returns 1234
ParseInt (" 0 xa "); / / returns 10
The parseInt (" 22.5 "); / / returns 22
The parseInt (" blue "); / / returns NaN

The parseInt() method also has a base mode that converts binary, octal, hexadecimal, or any other base string into an integer. The base is specified by the second parameter of the parseInt() method, as shown in the following example:

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 a leading 0, it is best to use base 10 so that you do not accidentally get an octal value. Such as:
The parseInt (" 010 "); / / returns 8
The parseInt (" 010 ", 8); / / returns 8
The parseInt (" 010 ", 10); / / returns 10

The parseFloat() method is handled similarly to the parseInt() method.
Another difference with the parseFloat() method is that strings must represent floating points in decimal form, and parseFloat() has no base mode.

Here's an example using the parseFloat() method:
1234 parseFloat (" blue "); / / returns 1234.0
The parseFloat (" 0 xa "); / / returns NaN
The parseFloat (" 22.5 "); / / returns 22.5
The parseFloat (" 22.34.5 "); / / returns 22.34
The parseFloat (" 0908 "); / / returns 908
The parseFloat (" blue "); / / returns NaN

2. Cast

You can also handle the type of the converted value using type casting. Use a cast to access a particular value, even if it is of another type.
The three casts available in ECMAScript are as follows:
Boolean(value) -- converts a given value to a Boolean;
Number(value) -- converts a given value to a Number(either an integer or a floating point Number);
String(value) -- converts a given value to 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 digit, or an object. If the value is an empty string, the number 0, undefined, or null, it returns false.

You can use the following code snippet to test a Boolean type cast.

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

The cast of Number() is similar to that of the parseInt() and parseFloat() methods, except that it converts the entire value instead of a partial value. Here's an example:

Use the results
The Number 0 (false)
Number 1 (true)
NaN Number (undefined)
The Number 0 (null)
Number (" 5.5 ") 5.5
Number 56 (" 56 ")
NaN Number (5.6.7 ")
NaN Number (new Object ())
Number (100) 100    

The last cast method, String(), is the simplest, as shown in this example:

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

For example, if you look at it, you'll see.
< Script>
Var STR = '012.345';
Var x = STR 0;
X = x * 1;
< / script>

The above example takes advantage of the weak type of js, only the arithmetic operation, to achieve the string to the number of the type conversion, but this method is not recommended


Related articles: