17

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.

putvande
  • 15,068
  • 3
  • 34
  • 50
user2627383
  • 199
  • 1
  • 1
  • 6
  • Use [`Number`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number#Function_syntax) to convert strings to numbers. [`parseFloat`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) can be used when you need to _parse_ numbers from a string — the semantics of _parsing_ and _converting_ are different; read the docs to understand the difference. If you really need integers, use [`parseInt`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/parseInt), but please [_with_ the second parameter, `10`](/q/16880327/4642212). – Sebastian Simon Jul 19 '21 at 19:36

6 Answers6

24

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

Syntax:

 parseInt(string, radix);
 parseFloat(string); 
  • string: the string expression to be parsed as a number.
  • radix: (optional, but highly encouraged) the base of the numeral system to be used - a number between 2 and 36.

Example:

 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.

trincot
  • 317,000
  • 35
  • 244
  • 286
Anurag-Sharma
  • 4,278
  • 5
  • 27
  • 42
  • 2
    -1 for the following reasons: parseFloat() does not have second argument (read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat), omitting radix in parseInt is a bad practice (see explanation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) and finally: conversion is not necessary to perform calculations (JavaScript is weakly typed, proof: http://jsfiddle.net/ehhfE/). – Tadeck Jul 28 '13 at 12:15
  • @Kadima, I reverted part of your proposed edit: the second argument to the *prompt* function is not the radix, but the default value. – trincot Aug 17 '16 at 18:58
  • @trincot Yep, you're right. Sorry, been staring at a computer screen too long. – Kadima Aug 17 '16 at 19:16
11

JavaScript will "convert" numeric string to integer, if you perform calculations on it (as JS is weakly typed). But you can convert it yourself using parseInt or parseFloat.

Just remember to put radix in parseInt!

In case of integer inputs:

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

In case of float:

var x = parseFloat(prompt("Enter a Value", "0"));
var y = parseFloat(prompt("Enter a Value", "0"));
Tadeck
  • 132,510
  • 28
  • 152
  • 198
5
var xInt = parseInt(x)

This will return either the integer value, or NaN.

Read more about parseInt here.

Community
  • 1
  • 1
Raul Rene
  • 10,014
  • 9
  • 53
  • 75
2

You can use parseInt() but, as mentioned, the radix (base) should be specified:

x = parseInt(x, 10);
y = parseInt(y, 10);

10 means a base-10 number.

See this link for an explanation of why the radix is necessary.

Community
  • 1
  • 1
Andy G
  • 19,232
  • 5
  • 47
  • 69
0

Working Demo Reading more Info

parseInt(x) it will cast it into integer

x = parseInt(x);
x = parseInt(x,10); //the radix is 10 (decimal)

parseFloat(x) it will cast it into float

Working Demo Reading more Info

x = parseFloat(x);

you can directly use prompt

var x = parseInt(prompt("Enter a Number", "1"), 10)
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
-1

You have to use parseInt() to convert

For eg.

  var z = parseInt(x) + parseInt(y);

use parseFloat() if you want to handle float value.

Uttam K C
  • 204
  • 2
  • 4