+ 1

How do i convert string into number in Javascript? (solved)

Hi. I am trying to make a calculation based on input values from a form type="numer". However in my output i noticed i get result a a string. for example if input x =2 and input y=2 and my output is set to x+y all i get is 22 instead of 4. I know its becouse of string values, but i am unable to get it converted. I have tried var height=parseInt(document.getElementById("heightInput").value); but this does not work for me.

22nd Jan 2017, 9:28 PM
Petras Vizgintas
Petras Vizgintas - avatar
18 odpowiedzi
+ 7
Maybe c = +a + +b UPD: It defenetly works Look in my codes, i have an example of cinvertion
23rd Jan 2017, 5:08 AM
WittyBit
WittyBit - avatar
+ 7
Pls show full js, that you are tryed to run
23rd Jan 2017, 10:14 AM
WittyBit
WittyBit - avatar
+ 6
It works. If you have x = "2" and y = "2", x * 1 + y * 1 will be 4. What's the formula for that KPI thing?
23rd Jan 2017, 7:11 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 4
The easiest way, x * 1 + y * 1 = 4.
23rd Jan 2017, 1:47 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 3
Hi. Have you tried Number() function? It can convert many type of objects to a number. :-) ________ var a = 3; var b = "6"; var notConverted = a + b; // 36 var Converted = a + Number(b) // 9 :-)
22nd Jan 2017, 9:38 PM
Heroes Killer
Heroes Killer - avatar
+ 3
Could you post me part of your code, where you have / get both values and then converting?
22nd Jan 2017, 10:10 PM
Heroes Killer
Heroes Killer - avatar
+ 3
Number function works correctly, you have to do something wrong. Did you really try this?: var height = document.getElementById("heightInput").value; var weight = document.getElementById("weightInput").value; var KPI = Number(height) + Number(weight); See example on the photo: http://i.imgur.com/vQEiNoM.png
23rd Jan 2017, 9:38 AM
Heroes Killer
Heroes Killer - avatar
+ 3
I think the best way is to get the value of the input first and save it into a variable. You can then use the parseInt function on that variable. var strHeight=document.getElementById("heightInput").value; var strWeight=document.getElementById("weightInput").value; height = parseInt(strHeight,10) weight = parseInt(strWeight,10) make sure to add 10 because it tells the radix(base) to convert the string to.
24th Jan 2017, 9:48 AM
Temitope Yussuph
Temitope Yussuph - avatar
+ 2
Have you tried something like this? var height = document.getElementById("heightInput").value; var weight = document.getElementById("weightInput").value; var KPI = Number(height) + Number(weight);
23rd Jan 2017, 6:17 AM
Heroes Killer
Heroes Killer - avatar
+ 2
var intValue = parseInt(string);
23rd Jan 2017, 11:25 PM
Avesta XYZ
Avesta XYZ - avatar
+ 1
Hi HK. I have now tried to use this function you mentioned and result was string anyway 😑
22nd Jan 2017, 9:52 PM
Petras Vizgintas
Petras Vizgintas - avatar
+ 1
unfortunately x*1+y*1 does not work aswell. Here is form code where i get values from. <form id="kpidata"> <label for="Your weight">Your weight here: </label> <input id="weightInput" type="number" name="weight" placeholder="weight in kg" /> </br> <label for="Your height">Your height here:</label> <input id="heightInput" type="number" name="height" placeholder="height in cm" /> <input type="button" onclick="kpicalc()" value="Calculate your KPI"> </form>
23rd Jan 2017, 4:55 AM
Petras Vizgintas
Petras Vizgintas - avatar
+ 1
Michael, thanks for your suggestion, but it does not work still. I have tried all the methods mentioned here and they all work under normal conditions. For sme reason it does not in what i am trying to do. As you can see in code sample i use for getting values on clicking submit button function is activated, function itself set values from boxes to variables and should make a sum. var height=document.getElementById("heightInput").value; var weight=document.getElementById("weightInput").value; var result=height+weight; and result i get is 22 if both of inputs are 2, still no luck in converting them :(
23rd Jan 2017, 8:15 AM
Petras Vizgintas
Petras Vizgintas - avatar
+ 1
HK, yes i did, i have published the code for you to access and try it if you want to. But Number(height) + Number(weight) does not work for some reason.
23rd Jan 2017, 9:50 AM
Petras Vizgintas
Petras Vizgintas - avatar
+ 1
Michael it's published now and it was solved now. I will keep it up for now so you can see the code if you want.. BTW thanks all for helping this noob 😆
23rd Jan 2017, 10:16 AM
Petras Vizgintas
Petras Vizgintas - avatar
+ 1
hi I want to create sign in form
24th Jan 2017, 6:57 AM
Om Sikarwar
Om Sikarwar - avatar
+ 1
using typecasting or string of inbiuld function
24th Jan 2017, 3:12 PM
Pradeep K.V.S. Maurya
Pradeep  K.V.S.  Maurya - avatar
+ 1
Hey guys I just updated my blog Please check out my blog https://www.programmingrofl.blogspot.in
24th Jan 2017, 7:16 PM
Saurabh Kanswal
Saurabh Kanswal - avatar