# [JavaScript] convert numerical value to numerical value

2022-04-29 05:24:5051CTO

number()：

Convert a numeric value to a numeric value
If it's a Boolean value , Or be converted to 1, Or be converted to 0
If it's a number , So how much should it be , How much?
If it is null value , Will be converted to 0
If it is undefined, Will be converted to NaN
String to number ：
If the string contains only numbers , Then it will only be converted to decimal
If the string is empty , Will be converted to 0
If there are letters , Then it will be converted to NaN
If the string is octal , Then you will ignore the previous 0, But if it is 16 In the case of decimal system , Will be converted to the corresponding decimal number

parseInt()：

This function will look more at whether there are numbers , If there is, it will be converted into numbers ; If the string is empty , Will be converted to NaN. The other thing is that , If it is 3.14, Then it will be converted into 3.
in fact , We parseInt() The function provides a second argument , Specifies the conversion to hexadecimal .

parseFloat()：

This function can only parse 10 Base number , So there is no second parameter . Converts a string with a decimal point to a decimal point

stay JavaScript in , There are generally three ways of numerical conversion ：

One 、Number(param) function ：param Can be used for any data type
1.1 param yes Boolean value ,true and false Convert to 1 and 0;
1.2 param Numerical value , It's just a simple pass in and return
1.3 param yes null and undefined, Return respectively 0 and NaN

toString()  Method ： With  ​ character string ​ Returns an array of , All number methods can be used for any type of number .

```

var x = 11;

x.toString();            //  Dependent variable  x  return  11

(11).toString();        //  From the text  11  return  11

(10 + 1).toString();   //  From expression  10 + 1  return  11

1.

2.

3.

4.

```

![](<> " Click and drag to move ")

Number() : Return to digital , From the transformation of its parameters . Can be used to JS Variables are converted to numeric values :

```

x = true;

Number(x);        //  return  1

x = false;

Number(x);        //  return  0

x = new Date();

Number(x);        //  return  1404568027739

x = "10"

Number(x);        //  return  10

x = "10 20"

Number(x);        //  Can't convert to numbers   Then return to  NaN

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

```

![](<> " Click and drag to move ")

parseInt() Conversion rules

because Number() Function in the conversion of string is more complex and unreasonable , So when dealing with integers, it's more common to use \
parseInt() function .parseInt() Function to convert a string , More to see if it fits the numerical model . It ignores words \
The space before the string , Until you find the first non space character . If the first character is not a numeric character ,parseInt()\
It will return NaN; in other words , use parseInt() Converting an empty string returns NaN（Number() Returns... For null characters 0）. Such as \
If the first character is a numeric character ,parseInt() Will continue to parse the second character , Until all subsequent characters are resolved or encountered \
A non numeric character . for example ,"1234blue" Will be converted to 1234, because "blue" Will be completely ignored . Similarly ,"22.5"\
Will be converted to 22, Because the decimal point is not a valid numeric character .
The specified cardinality will affect the output of the transformation . for example ：

```

var num1 = parseInt("10", 2); //2 （ Parse by binary ）

var num2 = parseInt("10", 8); //8 （ Parse in octal ）

var num3 = parseInt("10", 10); //10 （ Decimals ）

var num4 = parseInt("10", 16); //16 （ Parse in hexadecimal ）

1.

2.

3.

4.

```

![](<> " Click and drag to move ")
\

copyright notice
author[51CTO],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/116/202204261035255827.html