Different ways to convert string to number in JavaScript

    Jul 21, 2021       by Suraj Roy

In this article, We will see different ways to convert string type values to numbers.

1. parseInt()

This method accepts a string parameter and converts it into an integer type value. If passed input value in not convertable to number then it returns NaN.

Case 1: If the input value is number

let str1 = '123';

console.log(parseInt(str1)); // Output: 123

 

Case 2: If the input value is a combination of number and non-number characters.

let str2 = '123xyz';

console.log(parseInt(str2)); // Output: 123

 

Case 3: If the input value is a combination of number and non-number characters.

let str3 = 'xyz123';

console.log(parseInt(str3)); // Output: NaN

 

Case 4: If the input is emptry string

let str4 = '';

console.log(parseInt(str4)); // Output: NaN

 

Case 5: If the input is string with decimal value in it

let str5 = '11.5';

console.log(parseInt(str5)); // Output: 11

 

2. parseFloat()

This method accepts a string parameter and converts it into a floating point type value. If the passed input value is not Convertable to a number then it returns NaN.

Case 1: If the input value is number

let str1 = '123';

console.log(parseFloat(str1)); // Output: 123

 

Case 2: If the input value is a combination of number and non-number characters.

let str2 = '123xyz';

console.log(parseFloat(str2)); // Output: 123

 

Case 3: If the input value is a combination of number and non-number characters.

let str3 = 'xyz123';

console.log(parseFloat(str3)); // Output: NaN

 

Case 4: If the input is emptry string

let str4 = '';

console.log(parseFloat(str4)); // Output: NaN

 

3. Number()

The function accepts a string parameter and converts it to an integer or floating point number. If the passed input value is not Convertable to a number then it returns NaN.

Case 1: If the input value is number

let str1 = '123';

console.log(Number(str1)); // Output: 123

 

Case 2: If the input value is a combination of number and non-number characters.

let str2 = '123xyz';

console.log(Number(str2)); // Output: NaN

 

Case 3: If the input value is a combination of number and non-number characters.

let str3 = 'xyz123';

console.log(Number(str3)); // Output: NaN

 

Case 4: If the input is emptry string

let str4 = '';

console.log(Number(str4)); // Output: 0

 

4. Using Unary Operator (+)

In this approach, The unary plus operator (+) is placed before a string value which converts it to an integer or floating point number. If the passed input value is not Convertable to a number then it returns NaN.

Case 1: If the input value is number

let str1 = '123';

console.log(+str1); // Output: 123

 

Case 2: If the input value is a combination of number and non-number characters.

let str2 = '123xyz';

console.log(+str2); // Output: NaN

 

Case 3: If the input value is a combination of number and non-number characters.

let str3 = 'xyz123';

console.log(+str3); // Output: NaN

 

Case 4: If the input is emptry string

let str4 = '';

console.log(+str4); // Output: 0

 

5. Multiply by 1 (*1)

It works similar the above approach.

Syntax:

let str1 = '123';

console.log(str1*1); // Output: 123

 

6. Math.floor()

Like others, This method also accepts a string parameter and converts it into an integer that is rounded down to the lowest whole number. If passed input value in not Convertable to a number then it returns NaN.

Case 1: If the input value is number

let str1 = '123';

console.log(Math.floor(str1)); // Output: 123

 

Case 2: If the input value is a combination of number and non-number characters.

let str2 = '123xyz';

console.log(Math.floor(str2)); // Output: NaN

 

Case 3: If the input value is a combination of number and non-number characters.

let str3 = 'xyz123';

console.log(Math.floor(str3)); // Output: NaN

 

Case 4: If the input is emptry string

let str4 = '';

console.log(Math.floor(str4)); // Output: 0

 

Case 5: If the input is decimal value

let str5 = '11.7';

console.log(Math.floor(str5)); // Output: 11

 

7. Math.round()

Like others, This method also accepts a string parameter and converts it into an integer that is rounded to the closest whole number. If passed input value in not Convertable to a number then it returns NaN.
 

Case 1: If the input value is number

let str1 = '123';

console.log(Math.round(str1)); // Output: 123

 

Case 2: If the input value is a combination of number and non-number characters.

let str2 = '123xyz';

console.log(Math.round(str2)); // Output: NaN

 

Case 3: If the input value is a combination of number and non-number characters.

let str3 = 'xyz123';

console.log(Math.round(str3)); // Output: NaN

 

Case 4: If the input is emptry string

let str4 = '';

console.log(Math.round(str4)); // Output: 0

 

Case 5: If the input is decimal value

let str5 = '11.7';

console.log(Math.round(str5)); // Output: 12, Because passed value is closest to 12

 

Case 6: If the input is decimal value

let str5 = '11.4';

console.log(Math.round(str5)); // Output: 11, Because passed value is closest to 11

 

8. Using double tilde (~~) Operator

 

Case 1: If the input value is number

let str1 = '123';

console.log(~~str1); // Output: 123

 

Case 2: If the input value is a combination of number and non-number characters.

let str2 = '123xyz';

console.log(~~str2); // Output: 0

 

Case 3: If the input value is a combination of number and non-number characters.

let str3 = 'xyz123';

console.log(~~str3); // Output: 0

 

Case 4: If the input is emptry string

let str4 = '';

console.log(~~str4); // Output: 0

 
 

Case 5: If the input is string(with decimal value in it)

let str5 = '11.4';

console.log(~~str5); // Output: 11

 

Let me know your thoughts over email pankaj.itdeveloper@gmail.com. I would love to hear them and If you like this article, share it with your friends.

Thanks!


WHAT'S NEW

Find other similar Articles here: