Jquery convert string to number using parseInt() function

Many time we developed any webpage sometime there is a requirement where we need to convert string to a number,  it is a very simple process to do it Javascript provides its own string to number converter function.
                               

Jquery convert string to number

1var str_val = '234';
2var num_val = parseInt(str_val); 



parseInt() takes 2 parameter string and radix, the first parameter which value to be parsed, then returns an integer value or NaN. "radix" is used to specify which numeral system to be used for example a radix of 10 converts from a decimal number, 8 converts from octal, 16 from hexadecimal, and so on.

1parseInt(string, radix)

If you don't pass the radix parameter Javascript or leave it 0, then in this case javascript behaves like this.
  • If the string is not empty and begins with any value then radix would be 10 (Decimal) or 8 (octal).
  • If the string begins with '0x' or '0X' (Uppercase and lowercase), then the radix would be 16 (Hexadecimal).
Note:
If the given string is not get converted into an integer value, parseInt() returns NaN value. Some best Example
parseInt('123_456')
// 123

Still, if you have any doubt, feel free to comment below.

Post a Comment

Post a Comment (0)

Previous Post Next Post