This article will explain in detail how javascript converts strings into numbers. I think it is very practical, so I will share it with you as a reference. I hope you can gain something after reading this article.

Conversion methods: 1. Use operators such as “-“, “*”, “/”, “%”, “++”, “–“; 2. Use the “Number (value)” **statement; 3. Use ” parseInt(stringNum)” statement; 4. Use the “parseFloat(stringNum)” statement.**

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

**javascript convert string to number**

**Method 1: Use operators such as -, *, /, %, , –, ++**

**etc.**

JavaScript automatically converts strings to numbers, or NaN for those that cannot be converted to numbers. E.g:

1 2 3 4 5 6 |
alert("30"/5); //Division operation, the result is: 6 alert("15"-5); //Subtraction, the result is: 10 alert("20"*"a"); //Multiplication, the result is: NaN alert("20"%"3"); //modulo operation, the result is: 2 |

1 2 3 4 5 6 7 8 |
var num1 = "6"; var num2 = "6"; var num3 = "a"; alert(++num1); //Convert the string to a number and then perform the ++ operation, the result is: 7 alert(--num2); //Convert the string to a number and perform the -- operation, the result is: 5 alert(++num3); //The string cannot be converted to a number, the result is: NaN |

**Method 2: Use the Number() function**

Number() function can convert the parameter to a number

Use the following format:

1 2 3 |
Number(value) |

Number() converts the parameter value as a whole. When the parameter value contains any symbol that cannot be converted to a number, the conversion fails and NaN is returned, otherwise it returns the converted number.

When Number() performs numeric conversions on arguments, follow some of the following rules:

- If the parameter contains only numbers, it will be converted to decimal numbers, ignoring the leading 0 and leading spaces; if the number is preceded by -, the – will remain in the conversion result; if the number is preceded by +, the + sign will be deleted after conversion;
- If the parameter contains a valid floating-point number, it will be converted to the corresponding floating-point number, ignoring the leading 0 and leading spaces; if the number is preceded by -, the – will remain in the conversion result; if the number is preceded by +, it will be deleted after conversion + sign;
- If the parameter contains a valid hexadecimal number, it will be converted to a decimal number of the corresponding size;
- If the parameter is an empty string, it will be converted to 0;
- If the argument is a boolean, convert true to 1 and false to 0;
- If the parameter is null, it will be converted to 0;
- If the parameter is undefined, it will be converted to NaN;
- If the parameter is a Date object, it will be converted to the number of milliseconds from January 1, 1970 to when the conversion was performed;
- Converts to NaN if the argument is a function, an array object with more than two elements, and an object other than a Date object;

Converts to NaN if a special symbol or non-numeric character other than space, + and – is included in front of the parameter, or if a special symbol or non-numeric character including space, + and – is included in the middle of the parameter.

Conversion example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
alert(Number("0010")); //Remove two leading 0s, the result is: 10 alert(Number("+010")); //Remove the leading 0 and +, the result is: 10 alert(Number("-10")); //Retain the "-" number after conversion, the result is: -10 alert(Number('')); //The conversion result of an empty string is: 0 alert(Number(true)); //The conversion result of the boolean value true is: 1 alert(Number(null)); //The conversion result of null value is: 0 var d = new Date(); //Create a Date object alert(Number(d)); //Convert the Date object, the result is 1970.1.1 to the number of milliseconds when the conversion was performed: 1511351635179 alert(Number("100px")); //The parameter contains the character px that cannot be converted to a number, the result is: NaN alert(Number("100 01")); //The parameter contains spaces, so the entire parameter cannot be converted, the result is: NaN alert(Number("100-123")); //The parameter contains "-", so the entire parameter cannot be converted, the result is: NaN var a; //declare the variable alert(Number(a)); //The variable a is not assigned a value, so the value of a is undefined, and the result of converting undefined is: NaN var fn = function (){alert(1);}; //Create a function object alert(Number(fn)); //Conversion function, the result is: NaN alert(Number(window)); //Convert the window object, the result is: NaN |

From the above example, we can also see that Number() is converted as a whole, and any illegal characters in any place will cause the conversion to fail. The two functions that will be introduced next differ from Number() in that the conversion is performed bit by bit from left to right. If any bit cannot be converted, the conversion will be stopped immediately, and the value that has been successfully converted will be returned at the same time.

**Method 3: Use the parseInt() function**

The parseInt() function can convert the argument to an integer

Use the following format:

1 2 3 |
parseInt(stringNum,[radix]) |

The stringNum parameter is a string that needs to be converted into an integer; the radix parameter is a number between 2 and 36, which represents the hexadecimal number of the stringNum parameter. When the value is 10, it can be omitted.

The role of parseInt() is to parse the stringNum string parameter in base radix into a decimal number. If the stringNum string does not start with a legal character, NaN will be returned; if an illegal character is encountered during the parsing process, the parsing will be stopped immediately and the parsed value will be returned.

parseInt() follows these rules when parsing strings as integers:

- When parsing a string, the spaces before and after the string will be ignored; if the string is preceded by -, the – will remain in the conversion result; if the number is preceded by +, the + sign will be deleted after conversion;
- If the string is preceded by special symbols other than spaces, + and – or non-numeric characters other than a~f (or A~F), the string will not be parsed and the return result is NaN;
- When the string contains special symbols such as spaces, +, – and decimal point “.” or non-numeric characters, parsing will stop when these characters are encountered, and return the parsed result;
- If the string is the empty string, the result is NaN.

Conversion example:

1 2 3 4 5 6 7 8 9 10 |
alert(parseInt("1101",2)); //The result of parsing the 1101 string in base 2 is: 13 alert(parseInt("a37f",16)); //The result of parsing a37f string in base 16 is: 41855 alert(parseInt("123")); //The result of parsing the 123 string in base 10 is: 123 alert(parseInt(" 123")); //The space before the string will be ignored, the result is: 123 alert(parseInt("12 3")); //The string contains spaces, stop when parsing to spaces, the result is 12 alert(parseInt("12.345")); //The string contains a decimal point, the parsing stops when the decimal point is reached, and the result is 12 alert(parseInt("xy123")); //The string contains a non-numeric character "x" in front of it, which cannot be parsed, and the return result is: NaN alert(parseInt("123xy4")); //The string contains the non-numeric character "xy", stop when parsing to "x", the result is: 123 |

From the above example, we can see that when parseInt() parses floating-point numbers, the fractional data will be truncated. In this case, we need to use parseFloat(), which will be described below, instead of parseInt().

**Method 4: Use the parseFloat() function**

The parseFloat() function can convert the parameter to a floating point number

Use the following format:

1 2 3 |
parseFloat (stringNum) |

The stringNum parameter is the string that needs to be parsed as a float.

The function of parseFloat() is to convert a string whose first digit is a number into a floating-point number. If the stringNum string does not start with a legal character, NaN will be returned; if an illegal character is encountered during the parsing process, the parsing will be stopped immediately and the parsed value will be returned.

parseFloat() follows these rules when parsing strings as integers:

- When parsing a string, the spaces before and after the string will be ignored; if the string is preceded by -, the – will remain in the conversion result; if the number is preceded by +, the + sign will be deleted after conversion; if the string is preceded by a decimal point. Convert The result will add 0 before the decimal point;
- If the string is preceded by excluding spaces, +, – and . Special symbols other than the string will not be parsed, and the return result is NaN;
- When the string contains special symbols such as spaces, + and – or non-numeric characters, parsing will stop when these characters are encountered and return the parsed result;
- When the string contains more than two decimal points, the parsing will stop when the second decimal point is reached, and the parsed result will be returned;
- If the string is the empty string, the result is NaN.

Conversion example:

1 2 3 4 5 6 7 8 9 10 11 |
alert(parseFloat("312.456"));//The result is: 312.456 alert(parseFloat("-3.12"));//The "-" in front of the string will be retained, the result is: -3.12 alert(parseFloat("+3.12"));//The "-" in front of the string will be retained, the result is: 3.12 alert(parseFloat(".12"));//Add 0 before the decimal point, the result is: 0.12 alert(parseFloat(" 3.12"));//Truncate the spaces in front of the string, the result is: 3.12 alert(parseFloat("312.4A56"));//The string contains non-numeric character A, stop when parsing to A, the result is: 312.4 alert(parseFloat("31 2.4A56"));//The string contains spaces, stop when parsing to spaces, the result is: 31 alert(parseFloat("31.2.5"));//The string contains two decimal points, stop when parsing to the second decimal point, the result is: 31.2 alert(parseFloat("a312.456"));//The string is preceded by a non-numeric character a, the parsing cannot be performed, and the result is: NaN |

The article on “how javascript converts strings into numbers” is shared here. I hope the above content can be helpful to everyone, so that you can learn more knowledge. If you think the article is good, please share it. Let more people see it.