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.
1var sVal = '234';
2var iNum = parseInt(sVal); //Output will be 234.
Related Post:
The syntax of parseInt() function is,
1parseInt(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.
1var sVal = '23 45 68';
2var iNum = parseInt(sVal); //Output will be 23.
This function will only return "NaN", if the first character cannot be converted to a number.
Read More Articles

0 Comments:

Posting Komentar