+ 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.
18 odpowiedzi
+ 7
Maybe c = +a + +b
UPD: It defenetly works
Look in my codes, i have an example of cinvertion
+ 7
Pls show full js, that you are tryed to run
+ 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?
+ 4
The easiest way, x * 1 + y * 1 = 4.
+ 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
:-)
+ 3
Could you post me part of your code, where you have / get both values and then converting?
+ 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
+ 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.
+ 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);
+ 2
var intValue = parseInt(string);
+ 1
Hi HK. I have now tried to use this function you mentioned and result was string anyway 😑
+ 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>
+ 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 :(
+ 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.
+ 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 😆
+ 1
hi I want to create sign in form
+ 1
using typecasting or string of inbiuld function
+ 1
Hey guys I just updated my blog
Please check out my blog https://www.programmingrofl.blogspot.in