To convert a string to an integer in JavaScript

How to convert a string into integer in JavaScript?

To convert a string to an integer parseInt(), Number(), and Unary operator(+) function is used in javascript. parseInt() function returns Nan( not a number) when the string doesn’t contain number. If a string with a number is sent, then only that number will be returned as the output. This function won’t accept spaces. If any particular number with spaces is sent, then the part of the number that presents before space will be returned as the output.

To convert a string into integer we can use parseInt(), Number() and Unary operator(+). Each of them is discussed with an example.

Using the parseInt() method

The syntax for parseInt() method is −

parseInt(string); parseInt(string,radix);
  • string is the string to be parsed.
  • radix is used to mention the base in Number System. 2 for binary, 8 for octal, 10 for decimal and 16 for hexadecimal.

This method returns the integer value that is parsed from the string.

Example

This is an example program to convert a string into an integer using parseInt() method.

     

To convert a string to an integer in JavaScript

Using the Number() method

The syntax for Number() method is

Читайте также:  Index html redirect to home html

Where, value can be of any primitive data type that is convertible to number, else it returns NaN. This method returns a number.

Example

This is an example program to convert a string into an integer using Number() method.

     

To convert a string into integer in JavaScript using Number()

On executing the above code, the following output is generated.

Using the unary operator

The syntax for unary plus operator is −

Where, op is an operand. The unary operator + is placed before the operand and tries to convert it into a number. A number is returned.

Example

This is an example program to convert a string into an integer using Unary operator +.

     

To convert a string into integer in JavaScript using Unary operator

On executing the above code, the following output is generated.

Источник

7 ways to convert a String to Number in JavaScript

parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned.
This method has a limitation though. If you parse the decimal number, it will be rounded off to the nearest integer value and that value is converted to string. One might need to use parseFloat() method for literal conversion.

myString = '129' console.log(parseInt(myString)) // expected result: 129 a = 12.22 console.log(parseInt(a)) // expected result: 12 

2. Using Number()

Number() can be used to convert JavaScript variables to numbers. We can use it to convert the string too number.
If the value cannot be converted to a number, NaN is returned.

Number("10"); // returns 10 Number(" 10 "); // returns 10 Number("10.33"); // returns 10.33 

3. Using Unary Operator (+)

The unary plus operator ( + ) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn’t already.

const x = 25; const y = -25; console.log(+x); // expected output: 25 console.log(+y); // expected output: -25 console.log(+''); // expected output: 0 

4. Using parseFloat()

parseFloat() parses a string and returns a number. Spaces are allowed. Only the first number is returned.

parseFloat("10"); // returns 10 parseFloat("10.33"); // returns 10.33 parseFloat("10 20 30"); // returns 10 parseFloat("10 years"); // returns 10 parseFloat("years 10"); // returns NaN 

5. Using Math.floor()

The Math.floor() function returns the largest integer less than or equal to a given number. This can be little tricky with decimal numbers since it will return the value of the nearest integer as Number.

str = '1222' console.log(Math.floor(str)) // returns 1222 a = 12.22 Math.floor(a) // expected result: 12 

6. Multiply with number

Multiplying the string value with the 1 which won’t change the value and also it will be converted to number by default.

str = '2344' console.log(str * 1) // expected result: 2344 

7. Double tilde (~~) Operator

str = '1234' console.log(~~str) // expected result: 1234 negStr = '-234' console.log(~~negStr) // expected result: -234 

Here is the comparison of the ways mentioned performance wise. Comment below if you know more methods.
Thank You

Top comments (17)

The double tilde method is actually something I never thought about, but is worth explaining.

The double tilde «operator» is not as much as an operator as it’s a double bitwise negation.

Let’s cast ’64’ to 64 using this method, so we do ~~’64’ . First we will evaluate ~’64’ . As bitwise operations work on binary, ’64’ is cast to a number. So ~64 .

64 in binary is 01000000 . ~ will negate all the bits so it becomes 10111111 , which is -65 since numbers in JavaScript are signed. Now we negate it again, which becomes 01000000 , which is 64 in decimal.

I previously stated that 10111111 is -63 , which is incorrect. It’s actually -65 . Sorry about that.

14 likes Like Comment button

Источник

Оцените статью