Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Metric Measuring System logic help

Status
Not open for further replies.

devondago

Programmer
Jan 22, 2003
38
0
0
US
I have a small script that i need to expand it....this would be used to calculate the metric system. the first one works.
<form>
F: <input type="text" onchange="eval('C.value = ' + this.form.C_expr.value)" value="32" name="F">
<input type="hidden" value="(212-32)/100 * C.value + 32 " name="F_expr">
<br>
C: <input type="text" onchange="eval('F.value = ' + this.form.F_expr.value)" value="0" name="C">
<input type="hidden" value="100/(212-32) * (F.value - 32 )" name="C_expr">
<br>
<input type="reset" value="reset" name="reset"> <input type="button" value="convert" name="">
</form>

I am using this small script to calculate fahrenheit to celcius works both ways...
can someone help this javascript newbie to come up with the same format to calculate
Height
inches to centimeters formula is _ inches x 254 cm
what i have so far.
<form>
I: <input type="text" onchange="eval('CM.value = ' + this.form.CM_expr.value)" value="32" name="I">
<input type="hidden" value="(212-32)/100 * CM.value + 32 " name="I_expr">
<br>
CM: <input type="text" onchange="eval('I.value = ' + this.form.I_expr.value)" value="0" name="CM">
<input type="hidden" value="100/(212-32) * (I.value - 32 )" name="CM_expr">
<br>
<input type="reset" value="reset" name="reset"> <input type="button" value="convert" name="">
</form>

volume
ounzes to milliliters formula is __oz x 30cc
<form>
O: <input type="text" onchange="eval('M.value = ' + this.form.M_expr.value)" value="32" name="O">
<input type="hidden" value="(212-32)/100 * M.value + 32 " name="O_expr">
<br>
M: <input type="text" onchange="eval('O.value = ' + this.form.O_expr.value)" value="0" name="M">
<input type="hidden" value="100/(212-32) * (O.value - 32 )" name="M_expr">
<br>
<input type="reset" value="reset" name="reset"> <input type="button" value="convert" name="">
</form>

Weight to kilograms formula ___lbs. divide by 2.2
<form>
<h3>Weight</h3>
W: <input type="text" onchange="eval('K.value = ' + this.form.K_expr.value)" value="2.2" name="W">
<input type="hidden" value=" * 1 K.value * 2.2 " name="W_expr">
<br>
K: <input type="text" onchange="eval('W.value = ' + this.form.W_expr.value)" value="1" name="K">
<input type="hidden" value=" /2.2 (W.value / 2.2)" name="K_expr">
<br>
<input type="reset" value="reset" name="reset"> <input type="button" value="convert" name="">
</form>
 
Would this work?
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Metric -> English</title>
<script type='text/javascript'>
function inToCm(ele)
{
ele.form.elements[(ele.name=='in')?'cm':'in'].value = (ele.name=='in') ? (ele.value*2.54).toFixed(3) : (ele.value/2.54).toFixed(3);
}
function ozToMl(ele)
{
ele.form.elements[(ele.name=='oz')?'ml':'oz'].value = (ele.name=='oz') ? (ele.value*30).toFixed(3) : (ele.value/30).toFixed(3);
}
function lbToKg(ele)
{
ele.form.elements[(ele.name=='lb')?'kg':'lb'].value = (ele.name=='lb') ? (ele.value/2.2).toFixed(3) : (ele.value*2.2).toFixed(3);
}
</script>
</head>
<body>
<div>
<form action=''>
<table>
<TR><TD>IN:</td><TD><input type="text" onchange="inToCm(this)" value="0" name="in"></td></tr>
<TR><TD>CM:</td><TD><input type="text" onchange="inToCm(this)" value="0" name="cm"></td></tr>
</table>
<table>
<TR><TD>Oz:</td><TD><input type="text" onchange="ozToMl(this)" value="0" name="oz"></td></tr>
<TR><TD>mL:</td><TD><input type="text" onchange="ozToMl(this)" value="0" name="ml"></td></tr>
</table>
<table>
<TR><TD>lb:</td><TD><input type="text" onchange="lbToKg(this)" value="0" name="lb"></td></tr>
<TR><TD>kg:</td><TD><input type="text" onchange="lbToKg(this)" value="0" name="kg"></td></tr>
</table>
</form>
</div>
</body>
</html>

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
Hi...
How about if I need to enter feet and inches to get centimeters. Or if I have only the centimeters and I want to get the feet and inches result?
below is what i have so far...any help would be appreciated.
<tr>
<td colSpan="3">
<h2>Height</h2>
</td>
</tr>
<tr>
<td vAlign="top" align="left">
<input type="text" onchange="inToCm(this)" size="4" value="0" name="ft">Feet
(ft)<br>
<input type="text" onchange="inToCm(this)" size="4" value="0" name="in">Inches
(in)
</td>
<td align="center"><input style="FONT-WEIGHT: bold; BACKGROUND: #00308f; CURSOR: pointer; COLOR: #ffffff; FONT-FAMILY: Verdana" type="button" value="<-convert->" name=""><br>&nbsp;
</td>
<td vAlign="top" align="left"><input type="text" onchange="inToCm(this)" size="4" value="0" name="cm">Centimeters (cm)<br>
<input style="FONT-WEIGHT: bold; BACKGROUND: #00308f; CURSOR: pointer; COLOR: #ffffff; FONT-FAMILY: Verdana"
type="reset" value="reset" name="reset">
</td>
</tr>
 
Just as a side note, did you know that Google will do these caluclations for you? Try typing in "what is 10 inches in centimetres?" in the search box. It works for many unit types.

What a shame it insists on spelling "centimetres" as "centimeters", however.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 

I have found that rather than just code code code... sometimes you can achieve more if you sit back and decide the scope of your project up front.

devondago... you have already changed the scope of the question once already (by introducing the desire to handle feet as well as just inches)... where will it stop?

You need to decide exactly what you are wanting to do - think about all the ramifications (indeed, you would have come across the feet/inches issue at that stage - altering the initial question... and most likely altering the solution that people come up with).

Have you considered any of these measurement conversions:
feet > millimetres
cups > litres
pounds > grams

Once you decide what you want to support, write up the requirements (and make sure you have given this a lot of thought) and then stick to them as you develop.

Take this advice as you will -- it was meant constructively.
Jeff
 
Jeff I hear you. my requirement where one thing before now they are what i can back to the forum for help. They should not change now. If you can help I would greatly appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top