How to convert string to integer in javascript / Various ways to convert string to integer in javascript

Overview

Converting a string to number(or integer) is a much required feature in any programming language. Suppose you have a json string coming from a web server to browser. This json string has a field named visitorCount and you want to increment 1 to this value every time the page loads. Now if you straightaway get its value and add 1 to it, it will be appended to the value instead of adding 1 to it. Thus an initial value of ‘25‘ in the json string will become 251, while it should have been 26. Luckily, javascript has many ways to do this and this post will guide through those methods.

 

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 str = ’21’;
var number = parseInt(str, 10); // result is 21

Tips:

  1. This method removes all leading zeroes from the result. Thus, if the string is ‘000101‘, the result will be 101.
  2. 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.
  3. If the first character is not convertible to an integer, parseInt will return NaN.
  4. 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 str = ‘123‘;
var number = Number(str); // returns 123

Tips:

  1. 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.
  2. A string in decimal number format is completely converted to its integer equivalent. Thus, ‘10.23‘ will be converted to 10.23.
  3. An empty string given inside Number constructor will result in 0. Thus Number(‘ ‘) will be 0.
  4. 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 str = ‘100‘;
var num = +s;
console.log(typeof num); //prints number

Tips:

  1. 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.
  2. Unary operator also converts string with decimal values to their corresponding numeric format. Example, ‘123.45‘ will be converted to 123.45.
  3. 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 str = ‘123’;
var num = Math.round(str); // returns 123
var decimalstr = ‘45.67’
var roundedValue = Math.round(decimalstr); // returns 46

Tips:

  1. 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.
  2. It also strips of all the leading zeroes from the string while converting. That is, ‘000123‘ will be converted to 123.
  3. 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.
  4. 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.

 

Mark Your Impression

Close Menu