In previous article, I mentioned Parse CSV file data into Array Using Javascript but in this article, I have mentioned how we can convert string to number (integer) in javascript using various possible ways.
1. Using parseInt()
method
The parseInt() function parses a string argument and returns an integer, it takes a String as a first argument, and a base to which that String will be converted to.
This method always returns an integer.
Syntax:
parseInt(string, radix)
Example to convert string to number
var number = parseInt("10");
But there is a problem. If you try to convert "010" using parseInt function, it detects as octal number, and will return number 8, so therefore you should pass radix when using parseInt()
- No prefix - If there isn't a prefix, the radix is 10 (decimal).
- 0 - If the prefix is 0, then the radix is 8 (octal). Though, this feature is deprecated.
- 0x - If the prefix is 0x, then the radix is 16 (hexadecimal).
Example:
let str = '112';
let floatStr = '112.56';
let binaryStr = '7';
let nanStr = 'hello';
parseInt(str); // 112
parseInt(floatStr); // 112
parseInt(binaryStr, 2); // 111 (Binary)
parseInt(nanStr); // NaN (Not a Number)
2. Using Number()
The Number()
function accepts a string parameter and converts it to an integer or floating point number, or returns NaN (Not a Number) if the input value doesn't represent a number.
Example:
var x = Number("1000");
Note: Note that Number('')
returns 0, even though most people wouldn't consider the empty string to represent a valid number.
So, before passing string to Number, you can check if it is number using isNAN() function.
3. Multiply by 1
You can multiply string value by 1 to convert it into Number.
This method is considered as fastest method to convert string to number using Javascript.
let str = '200';
let fltStr = '200.11';
let nanStr = 'hello';
str * 1; // 200
fltStr * 1; // 200.11
nanStr * 1; // NaN
'1000' * 1; // 1000
'202.15' * 1; // 202.15
4. Using .parseFloat()
The parseFloat()
function parses an argument (converting it to a string first if needed) and returns a floating point number.
Syntax:
parseFloat(string)
In the following all examples, parseFloat() returns 3.14
parseFloat(3.14);
parseFloat('3.14');
parseFloat(' 3.14 ');
parseFloat('314e-2');
parseFloat('0.0314E+2');
but this one return NAN
parseFloat('HH2');
5. Using Unary plus (+)
Unary operators work on single operands. The Unary (+) operator is no different, and it precedes it's operand.
The unary + operator converts its operand to Number type.
Example:
let str = '200';
let fltStr = '200.21';
let nanStr = 'hello';
+str // 200
+fltStr // 200.21
+nanStr // NaN
+'1000' // 2000
+10.25 // 20.25
While using Unary plus (+) operator easily converts string to number in Javascript, it is advised not to use it, since it doesn't make code more readable.
6. Using Math.Floor()
The Math.floor() function returns the largest integer less than or equal to a given number, but it can also accept string value and convert it to integer by rounding down to the lowest whole number, using Javascript.
Example:
Math.round('121')// 121
Math.round('12.5') // 12
Math.round('123abc')// NaN
Math.floor('5.05') // 5
You can also use Math.ceil()
and Math.round()
in similar way like above, to convert string to int in Javascript.
That's it, hope it helps.
You may also like to read:
Using Google Translate to translate language using HTML- Javascript.
Google places autocomplete example without Map in Javascript
Toggle Class (Javascript based, without jQuery)