Method 1: Using parseInt method
This method takes 2 arguments: a string which is to be converted to an integer and a radix or base of the number system in which the resulting number should be. In order to convert a string to an integer, this value should be 10. Example,
var number = parseInt(str, 10); // result is 21
- This method removes all leading zeroes from the result. Thus, if the string is ‘000101‘, the result will be 101.
- parseInt will keep on converting till it find an alphabetic character or one which cannot be converted to an integer. Example, ‘123$‘ will be converted to 123; ‘10d24‘ will be converted to 10.
- If the first character is not convertible to an integer, parseInt will return NaN.
- When converting a string in the form of a decimal number, it will return the whole number part and leave the digits after decimal. Example, ‘10.20‘ will be converted to 10.
Method 2: Using Number Object
Number object has a constructor which takes a string argument and returns its integer form. Example,
var number = Number(str); // returns 123
- Number object will only convert a string when it is fully convertible to an integer otherwise it will return NaN. Thus, ‘12km‘, ‘5t7‘, ‘kit‘ all be NaN.
- A string in decimal number format is completely converted to its integer equivalent. Thus, ‘10.23‘ will be converted to 10.23.
- An empty string given inside Number constructor will result in 0. Thus Number(‘ ‘) will be 0.
- First two letters of a string can be provided as the base from which the string should be converted to an integer. Thus, ‘0x1‘ will be converted to a number from hexa decimal form(result will be 16), ‘0b11‘ will be converted from binary format(result will be 3), ‘0o12‘ will be converted from octal format(result will be 10).
Method 3: Using Unary plus
Prepend any string which is in numeric format with plus(+) and the result will be a number. Example
var num = +s;
console.log(typeof num); //prints number
- Unary operator will not convert a string which contains a character not convertible to an integer and will return NaN. Examples are 12k, 30ps, ft56 etc.
- Unary operator also converts string with decimal values to their corresponding numeric format. Example, ‘123.45‘ will be converted to 123.45.
- Unary operator truncates any leading zeroes from the string. Example, ‘00129‘ will be converted to 129.
Method 4: Using Math.round
round function of Math class takes a string as argument and converts it into an integer. Besides converting a string to an integer, it can also round a decimal string to nearest whole number. Example,
var num = Math.round(str); // returns 123
var decimalstr = ‘45.67’
var roundedValue = Math.round(decimalstr); // returns 46
- Math.round converts a string only when it is completely in numeric format. If there is a single character which cannot be converted to an integer, then it returns NaN.
- It also strips of all the leading zeroes from the string while converting. That is, ‘000123‘ will be converted to 123.
- If the first two characters of the string are ‘0o’, then Math.round considers it as an octal number and converts it to an integer accordingly. Example, if the string is ‘0o12‘, then the resulting integer will be 10.
- If the first two characters of the string are ‘0x’, then Math.round considers it as a hexadecimal number and converts it to an integer accordingly. Example, if the string is ‘0x12‘, then the resulting integer will be 18.
Hope this post helped you out. Keep visiting for more such stuff.