preface

Through an interview question to extend the JavaScript string to number.

The interview questions

Is using the + or unary addition operator the fastest way to convert a string to a number?

According to [MDN documentation][1], + is the fastest way to convert a string to a number because it does nothing if the value is already a number

So how many ways do we know about converting strings to numbers? Let’s take a look at it one by one.

JavaScript string to number

1. parseInt

Definitions and Usage

The parseInt() function parses a string and returns an integer.

grammar

parseInt(string, radix)

parseInt("10");			/ / 10
parseInt("19".10);		/ / 19
parseInt("11".2);		/ / 3
parseInt("17".8);		/ / 15
parseInt("1f".16);		/ / 31
parseInt("010".2);              / / 2
// Use base 10
parseInt("010");		/ / 10 traps
parseInt('08');                 / / 8 trap
// Only the integer part of the string that begins with a number is read
parseInt('44.jpg');              / / 44 trap
parseInt('jpg12');              //NaN  
Copy the code

Note: The ECMAScript 5 specification no longer allows parseInt to be implemented in an environment where strings starting with 0 characters are used as octal values. If the radix is not specified or is 0, the arguments are assumed to be parsed in base 10, and if the values begin with character pairs 0x or 0x, they are assumed to be parsed in base 16.

ParseInt (num, 10) : If you do not know the type of the num attribute, do not use parseInt to convert a string to a number.

2. parseFloat

Definitions and Usage

The parseFloat() function parses a string and returns a floating point number.

This function specifies whether the first character in a string is a number. If so, the string is parsed until the end of the number is reached, and then the number is returned as a number, not as a string.

grammar

parseFloat(string)

parseFloat("10");/ / 10
parseFloat("10.33"); / / 10.33
// If there are Spaces in the string to separate numbers, only the first number will be read
parseFloat("34 45 66"); / / 34
parseFloat("60")/ / 60
// Only the integer part of the string that begins with a number is read
parseFloat("40 years");/ / 40
parseFloat("He was 40");//NaN
parseInt(-0xFF) / / - 255
parseInt("-0xFF") / / - 255
parseFloat(-0xFF) / / - 255
A negative hexadecimal number in a string is a special case
parseFloat("-0xFF") / / 0 trap
Copy the code

Note: parseFloat: Be careful when converting hexadecimal numbers. Do not use parseFloat if you do not know the type of object you are converting. A negative hexadecimal number in a string is a special case, and if you parse with parseFloat, the result is incorrect. To avoid NaN cases in your program, you should check the converted values.

3. According to a non

Strings can be converted to integers, but they are not floating point numbers. If it is a string conversion, it returns 0;

~ ~1.23 // 1 trap takes only integer parts~ ~"1.23" / / 1~ ~"23" / / 23~ ~"Hello world" // The 0 trap is valid only for integers, not string characters~ ~"0xFF"  / / 255~ ~ -0xff   / / - 255~ ~"-0xFF"  // the 0 trap cannot parse negative hexadecimal strings
Copy the code

Note: ~ is the inverse operation by bit, ~~ is the inverse operation twice. The function of ~~ is to remove the decimal part. The value of the bit operation must be an integer, and the result is also an integer. Therefore, the bit operation automatically becomes an integer.

Bitwise non: Use this to ensure that there are no characters in the input, only for integers

4.Number

Definitions and Usage

The Number() function converts the value of an object to a Number.

If the value of the object cannot be converted to a Number, the Number() function returns NaN.

grammar

Number(object)

Number: Hardly ever.

Number("023") / / 23 trap
Number("0XFF") //  255
Number("-0XFF") / / NaN trap
Number(023) //  19  
Copy the code

Note that 023 is actually an octal number and returns 19 no matter what you do; This is true for hexadecimal numbers without single or double quotes. Octal numbers with single and double quotes (starting with a zero character) are not converted to their decimal counterparts, but are only parsed as decimal numbers. For negative hexadecimal numbers in strings, Number is not parsed and is displayed as NaN.

5. Unary operators (emphasis)

+"023" //23 + is recommended
+"16"  / / 16
+"15" / / to 15
+"-0xFF" //NaN  
+"0xFF"  / / 255
"1.23" * 1  / / 1.23
"1.23" / 1  / / 1.23
"2.23" - 0  / / 2.23
"0xFF.jpg" / 1  // NaN 
Copy the code

The addition operator triggers three types of conversion:

  • Convert to the original value
  • Convert to numbers
  • Convert to string

Note: Unary operators are different from other parsing methods in that they return NaN if it is a NaN value. The + operator is basically used because it is not confusing. While the use of -0 is also good, it doesn’t do a very good job of expressing the intent of the conversion to a number. + is the fastest way to convert a string to a number.

conclusion

-hexadecimal number when a string is converted to a number. It is best to parse to numbers using parseInt with cardinality. What is explicitly known as a base number is also resolved to a number by parseInt with cardinality. Others suggest using the unary operator + to convert strings to numbers, which is also the fastest.