View Full Version : Simple Mathematics
flying
10-04-2002, 11:05 PM
Question:
Let's say one has a form where someone puts numbers in. And that form prints to webpage. Can one have the form then multiply, for instance, two numbers and then print the result to the webpage?
As always,
Flying (by the seat of my pants)
chrismiceli
10-05-2002, 12:45 AM
you could change the operator sign or make the user pick it someway but the last input is going to look like regular text.
<html>
<head>
<title>test</title>
<script language="javascript">
function calculate() {
test = parseInt(document.form1.text1.value) * parseInt(document.form1.text2.value);
document.fomr1.result.value = test;
}
</sciprt>
</head>
<body>
<form name="form1">
<input type="text" name="text1" value="">
<input type="text" name="text2" value="">
<input type="button" value="calculate" onClick="calculate()">
<input type"text" value="Answer" style="border-width:0px;">
</form>
</body>
</html>
whammy
10-05-2002, 02:48 AM
To answer your question: Yes.
But what kind of problems are you running into attempting this? In order for someone to help you, you have to be MUCH more specific - and you might want to read the posting rules.
If you are having a problem, please post the relevant code. Otherwise, how are we supposed to be able to help you? We are not mind readers. (At least in general) !
flying
10-05-2002, 05:11 PM
Whammy, I am fairly new to this so forgive my not asking the question correctly. The problem with posting the code is this: I have pages and pages of code because I have another script running in the form (a quadruple drop down). So I will do my best to explain without posting. (Since I am not adept at paring down).
I have three files. A html file that is a form. This form uses a .cgi file to write to another .html file. It is similar to a guestbook.
So the user writes to the form html file certain numbers and when done presses submit. The .cgi file takes the info as it has been put in and writes it to the second .html file.
Is there a way for me to have the second .html file not only reflect the numbers but also calculations with those numbers.
So, for example:
The user enters in the form - in one field the number 3 and in another the number 2.
Can the output .html show both the number 3 and the number 2 as well as the result of the multiplication - the number 6?
Any help would be greatly appreciated.
bcbasslet
10-07-2002, 12:47 AM
this works for integers
<html>
<head>
<title>test</title>
<script language="javascript">
function calculate() {
test = parseInt(document.form1.text1.value) * parseInt(document.form1.text2.value);
document.form1.result.value = test;
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="text1" value="" size="20">
<input type="text" name="text2" value="" size="20">
<input type="button" value="calculate" onClick="calculate()">
<input type="text" value="" name="result" size="20">
</form>
</body>
</html>
flying
10-07-2002, 05:47 PM
Many Thanks!!! That really helped but need something that will work with all numbers (ie., .5 in first field and 7) will only yield integers even with modification I attempted. Any suggestions?
Thanks again..
bcbasslet
10-07-2002, 06:22 PM
parseFloat will get the decimals as well.
math.Round(yournumber,numofdecplaces)
math.Round(37.456,1) produces 37.5
but...math.Round(37.3000, 2) produces 37.3
without the final 0.
makes me so mad! i don't know how to get around that yet.
i'm going to post that question today.
flying
10-07-2002, 07:55 PM
Okay, so using parseFloat instead of parseInt in the code above we get:
<html>
<head>
<title>test</title>
<script language="javascript">
function calculate() {
test = parseFloat(document.form1.text1.value) * parseFloat(document.form1.text2.value);
document.form1.result.value = test;
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="text1" value="" size="20">
<input type="text" name="text2" value="" size="20">
<input type="button" value="calculate" onClick="calculate()">
<input type="text" value="" name="result" size="20">
</form>
</body>
</html>
And it seem to work.
BUT... try it with .7 as your first number and 7 as the last number. It does not give the expected 49 but 4.8999999999999995 and same with .7 and 17 - or with .17 and 7. So far it is only with this combo of 7's. Any idea why and how to fix?
ALSO... where does the math.Round go in this coding? And how does it look? math.Round(test,2)?
Thanks for the help!
chrismiceli
10-07-2002, 10:32 PM
<html>
<head>
<title>test</title>
<script language="javascript">
function calculate() {
test = parseFloat(document.form1.text1.value) * parseFloat(document.form1.text2.value);
newtest = Math.round(test *100)/100 //edited it, was lowercase m, now caps
document.form1.result.value = newtest;
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="text1" value="" size="20">
<input type="text" name="text2" value="" size="20">
<input type="button" value="calculate" onClick="calculate()">
<input type="text" value="" name="result" size="20">
</form>
</body>
</html>
flying
10-07-2002, 11:37 PM
Still having the same problem with .7 and 7 and the rounding down to two decimal points?
chrismiceli
10-07-2002, 11:44 PM
sorry, the "m" in math should be "M" capitalized, sorry, goodluck :thumbsup:
flying
10-08-2002, 02:53 PM
Perfect! Thank you.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.