How to convert js strings into numbers and numbers into strings

quote

var i = parseInt('abc');
if (isNaN(i))
{
alert('NaN value');
}


Converting js strings into numbers

To convert strings into numbers, you need to use the parseInt function.
parseInt(string) : The function parses from the beginning of the string and returns an integer.


For example:
parseInt('123') : return 123 (int);
parseInt('1234xxx') : return 1234 (int);

if the number cannot be parsed, it will return a NaN value, which can be detected with the isNaN() function ;

Example:
var i = parseInt('abc');
if (isNaN(i))
{
alert('NaN value');
}

The same parseFloat function converts a string to a float.

Example: parseFloat('31.24abc') : return 31.24;


js numbers are converted into strings


To convert strings into numbers, use the toString method of the String class

Example:
var i = 10;
var s = i.toString();
alert(typeof s); //


The difference between the output String js numbers and strings The

addition of numbers in js and the connection of strings are all + symbols, so whether to add or connect strings depends on the type of variables.

Example:
var a = 'abc' + 'xyz'; //The value of a is: abcxyz, the string and the string are connected
var a = 10 + 5; //The value of a: 15, the number is plus
var a = ' abc' + 10; //The value of a: abc10, a string and a number, automatically convert 10 to a string
var a = 'abc' + 10 + 20 + 'cd'; //The value of a: abc1020cd
var a = 10 + 20 + 'abc' + 'cd'; //The value of a is: 30abccd, you can add numbers first, and then connect them.

Supplement :

js strings convert numbers. There are three main methods:

conversion function, forced type conversion, and weak type conversion using js variables.

1. Conversion function:

js provides two conversion functions, parseInt() and parseFloat(). 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:

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

The parseInt() method also has base mode, which can convert binary, octal, hexadecimal, or any other base string to an integer. The base is specified by the second parameter of the parseInt() method, for example:

parseInt("AF", 16); //returns 175
parseInt("10", 2); //returns 2
parseInt("10", ; //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 don't accidentally get an octal value. For example:
parseInt("010" ); //returns 8
parseInt("010", ; //returns 8
parseInt("010", 10); //returns 10

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

Here is an example of using the parseFloat() method:
parseFloat("1234blue"); //returns 1234.0
parseFloat("0xA" ); //returns NaN
parseFloat("22.
parseFloat("22.34.5"); //returns 22.34
parseFloat("0908"); //returns 908
parseFloat("blue"); //returns NaN

2. Type casting You

can also use type casting The type that handles the converted value. 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 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 will return true when the value to be converted is a string with at least one character, a non-zero number, or an object. 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);
Boolean(0); //false - zero
Boolean(new Object()); //true -

the coercion of object Number() is similar to that of the parseInt() and parseFloat() methods, except that it converts the entire value, not a partial value. The example is as follows:

Usage result
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 cast method String() is the simplest, 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, you will understand at a glance.
<script>
var str= '012.345 ';

x = x*1;
</script>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326837164&siteId=291194637