jQuery – Convert string to integer

Find code to convert String to Integer using jQuery. To convert, use JavaScript parseInt() function which parses a string and returns an integer.

var sVal = '234';
var iNum = parseInt(sVal); //Output will be 234.

Related Post:

The syntax of parseInt() function is,

parseInt(string, radix)

It takes 2 parameters. First parameter is value which needs to be converted. And second parameter "radix" is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a integer number.

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with any other value, the radix is 10 (decimal)

If you string contains spaces then only first number will be returned.

var sVal = '23 45 68';
var iNum = parseInt(sVal); //Output will be 23.

This function will only return "NaN", if the first character cannot be converted to a number.

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...