formatting problem with math in javascript.
I am creating a very simple calculator for a web page. basically it is (entered value - entered value * 4) and (entered value + entered value * 4).
the first (subtraction) works, but the second (addition) does not format properly and returnes both values as strings joined together (ex. 1.2340.525).
This function works:
function externalmax(){
if (maxafterpitch.value=="") maxafterpitch.value="0";
if (maxthickness.value=="") maxthickness.value="0";
var pinum=(maxafterpitch.value-(maxthickness.value*4))
show("The Maximum Suggested PrePlate Pitch Diameter Is:<br><input type=text size=20 value="+pinum+">. ");
}
this function does not work:
function internalmin(){
if (mindiameter2.value=="") mindiameter2.value="0";
if (maxthickness2.value=="") maxthickness2.value="0";
var pinum=((maxthickness2.value*4)+mindiameter2.value);
show("The Minimum Suggested PrePlate Pitch Diameter is:<br><input type=text size=20 value="+pinum+">. ");
}
(returns: 0.002.02500)
Is there a method to format math values returned in javascript?
Thanks for any help.
MP
Re: formatting problem with math in javascript.
Everything entered through user input is returned as strings, therefore you must parseInt() or parseFloat() your inputed values before you can use them in your calculations...
Re: formatting problem with math in javascript.
Thanks for your reply.
I'm totally new at javascript.
Can you give me an example of the syntax?
thanks
Re: formatting problem with math in javascript.
I wasn't sure what you were doing exactly, but this is what I assumed, i'm sure you can figure it out though. The reason your first function is working, but the second isn't, is because in your second function, you are adding, but the '+' operator is used for string concatenation also, so therefore you must parseInt() or parseFloat() your inputed values(I make it a habit of doing it to all of them). In your first function, there is no '+' operator therefore they are casted to the right values.
PHP Code:
<html>
<head>
<script type="text/javascript">
function externalMax()
{
var one = document.myForm.maxafterpitch;
var two = document.myForm.maxthickness;
if(one.value == "" || two.value == "")
{
one.value = 0;
two.value = 0;
}
else
{
var pinum = (parseFloat(one.value) - (parseFloat(two.value)*4));
alert(pinum);
}
}
function internalMin()
{
var three = document.myForm.mindiameter2;
var four = document.myForm.maxthickness2;
if(three.value == "" || four.value == "")
{
three.value = 0;
four.value = 0;
}
else
{
var pin = ((parseFloat(four.value)*4) + parseFloat(three.value));
alert(pin);
}
}
</script>
</head>
<body>
<form name="myForm" action="" method="">
1<input type="text" name="maxafterpitch">
2<input type="text" name="maxthickness"><br>
3<input type="text" name="mindiameter2">
4<input type="text" name="maxthickness2"><br>
<input type="button" value="Click" onclick="externalMax(); internalMin();">
</form>
</body>
</html>
Hopeully that makes some sense? :rolleyes:
Good luck;
Re: formatting problem with math in javascript.
I think I got it.
Thanks a lot!
:wave: