user2627383 - 3 months ago 18

jQuery Question

I was trying to do some simple mathematical calculations in HTML and jQuery and JavaScript, so I wanted to get input from user.

For input I tried doing this :

`var x = prompt("Enter a Value","0");`

var y = prompt("Enter a Value", "0");

But I am not able to perform any kind of calculations as these values are strings.

Please, can any one show me how to convert them into integers.

Answer

**parseInt()** or **parseFloat()** are functions in JavaScript which can help you convert the values into integers or float respectively .

Syntax : -

```
parseInt(string, radix);
parseFloat(string,radix);
```

**string** : - it can be either a variable or a string itself .

**radix** :- (Optional) It is a numeric value between 2 - 36 , it represents the numeric system to be used.(Not for parseFloat() ) .

Try this : -

```
var x = prompt("Enter a Value","0");
var y = prompt("Enter a Value", "0");
var num1 = parseInt(x);
var num2 = parseInt(y);
```

After this you can perform which ever calculations you want on them .

Source (Stackoverflow)

Comments