JavaScript

How to Convert String to Number in Javascript Example8 min read

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:

Method 2: Use the Number() function

Number() function can convert the parameter to a number

Use the following format:

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:

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:

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:

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:

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:

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.

Leave a Comment