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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting persons height from imperial to metric

Status
Not open for further replies.

jamert

Programmer
Dec 9, 2007
80
CA
Hi i failing at trying to convert a users input of height into centimeters and vice versa, please help, this is what i have so far, thanks

Code:
<HTML>
<HEAD>
<TITLE></TITLE>
<script>
function heightChg(feet,inch,cm1,cm2,daVal)
{
var temp1=0;
var temp2=0;
var temp3="";
var temp4="";
var temp5="";
var temp6=0;

if (daVal==1){
temp1 = (inch*1)/12;
alert(temp1);
temp2 = (feet*12)*2.54;
alert(temp2);
var feet2cm = temp1+temp2;
alert(feet2cm);
feet2cm = feet2cm.toFixed(2);  //eg: 130.48cm
temp3 = feet2cm.split(".",2);
document.getElementById('cm1').value=temp3[0];
document.getElementById('cm2').value=temp3[1];
} else if (daVal==2){
temp1 = (cm1*1)+(cm2*1/100);  //eg 130.48cm
if ((cm2*1/100)<0.09){temp1 = (cm1*1)+(cm2*1/10);}
temp2 = temp1*0.3937008;  //eg: 56.53845 inches
temp2=temp2/12;
temp3=temp2+"";
temp4=temp3.split(".",2);
document.getElementById('feet').value=temp4[0];
temp2=temp4[1];
temp2=(temp2+0.00000001);
temp2=((temp2.slice(0,2))/100);
temp1=temp2*12;
temp1=Math.round(temp1);
document.getElementById('inches').value=temp1;
}
}



</script>

</HEAD>

<BODY>
<form id="f1">
<P><SELECT SIZE="1" id="feet" NAME="feet" onchange="heightChg(this.value,document.getElementById('inches').value,document.getElementById('cm1').value,document.getElementById('cm2').value,1)">
  <OPTION VALUE="3">3</OPTION>
  <OPTION VALUE="4">4</OPTION>
  <OPTION VALUE="5">5</OPTION>
  <OPTION VALUE="6">6</OPTION>
  <OPTION VALUE="7">7</OPTION>
  <OPTION VALUE="8">8</OPTION>
</SELECT>feet&nbsp;&nbsp;&nbsp; <SELECT SIZE="1" NAME="inches" id="inches" onchange="heightChg(document.getElementById('feet').value,this.value,document.getElementById('cm1').value,document.getElementById('cm2').value,1)">
  <OPTION VALUE="1">1</OPTION>
  <OPTION VALUE="2">2</OPTION>
  <OPTION VALUE="3">3</OPTION>
  <OPTION VALUE="4">4</OPTION>
  <OPTION VALUE="5">5</OPTION>
  <OPTION VALUE="6">6</OPTION>
  <OPTION VALUE="7">7</OPTION>
  <OPTION VALUE="8">8</OPTION>
  <OPTION VALUE="9">9</OPTION>
  <OPTION VALUE="10">10</OPTION>
  <OPTION VALUE="11">11</OPTION>
  <OPTION VALUE="12">12</OPTION>
</SELECT>inch&nbsp;&nbsp; OR&nbsp;&nbsp;

<SELECT SIZE="1" NAME="cm1" id="cm1" onchange="heightChg(document.getElementById('feet').value,document.getElementById('inches').value,this.value,document.getElementById('cm2').value,2)">
<OPTION VALUE="40">40</OPTION>
<OPTION VALUE="41">41</OPTION>
<OPTION VALUE="42">42</OPTION>
<OPTION VALUE="43">43</OPTION>
<OPTION VALUE="44">44</OPTION>
<OPTION VALUE="45">45</OPTION>
<OPTION VALUE="46">46</OPTION>
<OPTION VALUE="47">47</OPTION>
<OPTION VALUE="48">48</OPTION>
<OPTION VALUE="49">49</OPTION>
<OPTION VALUE="50">50</OPTION>
<OPTION VALUE="51">51</OPTION>
<OPTION VALUE="52">52</OPTION>
<OPTION VALUE="53">53</OPTION>
<OPTION VALUE="54">54</OPTION>
<OPTION VALUE="55">55</OPTION>
<OPTION VALUE="56">56</OPTION>
<OPTION VALUE="57">57</OPTION>
<OPTION VALUE="58">58</OPTION>
<OPTION VALUE="59">59</OPTION>
<OPTION VALUE="60">60</OPTION>
<OPTION VALUE="61">61</OPTION>
<OPTION VALUE="62">62</OPTION>
<OPTION VALUE="63">63</OPTION>
<OPTION VALUE="64">64</OPTION>
<OPTION VALUE="65">65</OPTION>
<OPTION VALUE="66">66</OPTION>
<OPTION VALUE="67">67</OPTION>
<OPTION VALUE="68">68</OPTION>
<OPTION VALUE="69">69</OPTION>
<OPTION VALUE="70">70</OPTION>
<OPTION VALUE="71">71</OPTION>
<OPTION VALUE="72">72</OPTION>
<OPTION VALUE="73">73</OPTION>
<OPTION VALUE="74">74</OPTION>
<OPTION VALUE="75">75</OPTION>
<OPTION VALUE="76">76</OPTION>
<OPTION VALUE="77">77</OPTION>
<OPTION VALUE="78">78</OPTION>
<OPTION VALUE="79">79</OPTION>
<OPTION VALUE="80">80</OPTION>
<OPTION VALUE="81">81</OPTION>
<OPTION VALUE="82">82</OPTION>
<OPTION VALUE="83">83</OPTION>
<OPTION VALUE="84">84</OPTION>
<OPTION VALUE="85">85</OPTION>
<OPTION VALUE="86">86</OPTION>
<OPTION VALUE="87">87</OPTION>
<OPTION VALUE="88">88</OPTION>
<OPTION VALUE="89">89</OPTION>
<OPTION VALUE="90">90</OPTION>
<OPTION VALUE="91">91</OPTION>
<OPTION VALUE="92">92</OPTION>
<OPTION VALUE="93">93</OPTION>
<OPTION VALUE="94">94</OPTION>
<OPTION VALUE="95">95</OPTION>
<OPTION VALUE="96">96</OPTION>
<OPTION VALUE="97">97</OPTION>
<OPTION VALUE="98">98</OPTION>
<OPTION VALUE="99">99</OPTION>
<OPTION VALUE="100">100</OPTION>
<OPTION VALUE="101">101</OPTION>
<OPTION VALUE="102">102</OPTION>
<OPTION VALUE="103">103</OPTION>
<OPTION VALUE="104">104</OPTION>
<OPTION VALUE="105">105</OPTION>
<OPTION VALUE="106">106</OPTION>
<OPTION VALUE="107">107</OPTION>
<OPTION VALUE="108">108</OPTION>
<OPTION VALUE="109">109</OPTION>
<OPTION VALUE="110">110</OPTION>
<OPTION VALUE="111">111</OPTION>
<OPTION VALUE="112">112</OPTION>
<OPTION VALUE="113">113</OPTION>
<OPTION VALUE="114">114</OPTION>
<OPTION VALUE="115">115</OPTION>
<OPTION VALUE="116">116</OPTION>
<OPTION VALUE="117">117</OPTION>
<OPTION VALUE="118">118</OPTION>
<OPTION VALUE="119">119</OPTION>
<OPTION VALUE="120">120</OPTION>
<OPTION VALUE="121">121</OPTION>
<OPTION VALUE="122">122</OPTION>
<OPTION VALUE="123">123</OPTION>
<OPTION VALUE="124">124</OPTION>
<OPTION VALUE="125">125</OPTION>
<OPTION VALUE="126">126</OPTION>
<OPTION VALUE="127">127</OPTION>
<OPTION VALUE="128">128</OPTION>
<OPTION VALUE="129">129</OPTION>
<OPTION VALUE="130">130</OPTION>
<OPTION VALUE="131">131</OPTION>
<OPTION VALUE="132">132</OPTION>
<OPTION VALUE="133">133</OPTION>
<OPTION VALUE="134">134</OPTION>
<OPTION VALUE="135">135</OPTION>
<OPTION VALUE="136">136</OPTION>
<OPTION VALUE="137">137</OPTION>
<OPTION VALUE="138">138</OPTION>
<OPTION VALUE="139">139</OPTION>
<OPTION VALUE="140">140</OPTION>
<OPTION VALUE="141">141</OPTION>
<OPTION VALUE="142">142</OPTION>
<OPTION VALUE="143">143</OPTION>
<OPTION VALUE="144">144</OPTION>
<OPTION VALUE="145">145</OPTION>
<OPTION VALUE="146">146</OPTION>
<OPTION VALUE="147">147</OPTION>
<OPTION VALUE="148">148</OPTION>
<OPTION VALUE="149">149</OPTION>
<OPTION VALUE="150">150</OPTION>
<OPTION VALUE="151">151</OPTION>
<OPTION VALUE="152">152</OPTION>
<OPTION VALUE="153">153</OPTION>
<OPTION VALUE="154">154</OPTION>
<OPTION VALUE="155">155</OPTION>
<OPTION VALUE="156">156</OPTION>
<OPTION VALUE="157">157</OPTION>
<OPTION VALUE="158">158</OPTION>
<OPTION VALUE="159">159</OPTION>
<OPTION VALUE="160">160</OPTION>
<OPTION VALUE="161">161</OPTION>
<OPTION VALUE="162">162</OPTION>
<OPTION VALUE="163">163</OPTION>
<OPTION VALUE="164">164</OPTION>
<OPTION VALUE="165">165</OPTION>
<OPTION VALUE="166">166</OPTION>
<OPTION VALUE="167">167</OPTION>
<OPTION VALUE="168">168</OPTION>
<OPTION VALUE="169">169</OPTION>
<OPTION VALUE="170">170</OPTION>
<OPTION VALUE="171">171</OPTION>
<OPTION VALUE="172">172</OPTION>
<OPTION VALUE="173">173</OPTION>
<OPTION VALUE="174">174</OPTION>
<OPTION VALUE="175">175</OPTION>
<OPTION VALUE="176">176</OPTION>
<OPTION VALUE="177">177</OPTION>
<OPTION VALUE="178">178</OPTION>
<OPTION VALUE="179">179</OPTION>
<OPTION VALUE="180">180</OPTION>
<OPTION VALUE="181">181</OPTION>
<OPTION VALUE="182">182</OPTION>
<OPTION VALUE="183">183</OPTION>
<OPTION VALUE="184">184</OPTION>
<OPTION VALUE="185">185</OPTION>
<OPTION VALUE="186">186</OPTION>
<OPTION VALUE="187">187</OPTION>
<OPTION VALUE="188">188</OPTION>
<OPTION VALUE="189">189</OPTION>
<OPTION VALUE="190">190</OPTION>
<OPTION VALUE="191">191</OPTION>
<OPTION VALUE="192">192</OPTION>
<OPTION VALUE="193">193</OPTION>
<OPTION VALUE="194">194</OPTION>
<OPTION VALUE="195">195</OPTION>
<OPTION VALUE="196">196</OPTION>
<OPTION VALUE="197">197</OPTION>
<OPTION VALUE="198">198</OPTION>
<OPTION VALUE="199">199</OPTION>
<OPTION VALUE="200">200</OPTION>
<OPTION VALUE="201">201</OPTION>
<OPTION VALUE="202">202</OPTION>
<OPTION VALUE="203">203</OPTION>
<OPTION VALUE="204">204</OPTION>
<OPTION VALUE="205">205</OPTION>
<OPTION VALUE="206">206</OPTION>
<OPTION VALUE="207">207</OPTION>
<OPTION VALUE="208">208</OPTION>
<OPTION VALUE="209">209</OPTION>
<OPTION VALUE="210">210</OPTION>
<OPTION VALUE="211">211</OPTION>
<OPTION VALUE="212">212</OPTION>
<OPTION VALUE="213">213</OPTION>
<OPTION VALUE="214">214</OPTION>
<OPTION VALUE="215">215</OPTION>
<OPTION VALUE="216">216</OPTION>
<OPTION VALUE="217">217</OPTION>
<OPTION VALUE="218">218</OPTION>
<OPTION VALUE="219">219</OPTION>
<OPTION VALUE="220">220</OPTION>
<OPTION VALUE="221">221</OPTION>
<OPTION VALUE="222">222</OPTION>
<OPTION VALUE="223">223</OPTION>
<OPTION VALUE="224">224</OPTION>
<OPTION VALUE="225">225</OPTION>
<OPTION VALUE="226">226</OPTION>
<OPTION VALUE="227">227</OPTION>
<OPTION VALUE="228">228</OPTION>
<OPTION VALUE="229">229</OPTION>
<OPTION VALUE="230">230</OPTION>
<OPTION VALUE="231">231</OPTION>
<OPTION VALUE="232">232</OPTION>
<OPTION VALUE="233">233</OPTION>
<OPTION VALUE="234">234</OPTION>
<OPTION VALUE="235">235</OPTION>
<OPTION VALUE="236">236</OPTION>
<OPTION VALUE="237">237</OPTION>
<OPTION VALUE="238">238</OPTION>
<OPTION VALUE="239">239</OPTION>
<OPTION VALUE="240">240</OPTION>
<OPTION VALUE="241">241</OPTION>
<OPTION VALUE="242">242</OPTION>
<OPTION VALUE="243">243</OPTION>
<OPTION VALUE="244">244</OPTION>
<OPTION VALUE="245">245</OPTION>
<OPTION VALUE="246">246</OPTION>
<OPTION VALUE="247">247</OPTION>
<OPTION VALUE="248">248</OPTION>
<OPTION VALUE="249">249</OPTION>
<OPTION VALUE="250">250</OPTION>
</SELECT><strong>.</strong>
<SELECT SIZE="1" NAME="cm2" id="cm2" onchange="heightChg(document.getElementById('feet').value,document.getElementById('inches').value,document.getElementById('cm1').value,this.value,2)">
<OPTION VALUE="1">1</OPTION>
<OPTION VALUE="2">2</OPTION>
<OPTION VALUE="3">3</OPTION>
<OPTION VALUE="4">4</OPTION>
<OPTION VALUE="5">5</OPTION>
<OPTION VALUE="6">6</OPTION>
<OPTION VALUE="7">7</OPTION>
<OPTION VALUE="8">8</OPTION>
<OPTION VALUE="9">9</OPTION>
<OPTION VALUE="10">10</OPTION>
<OPTION VALUE="11">11</OPTION>
<OPTION VALUE="12">12</OPTION>
<OPTION VALUE="13">13</OPTION>
<OPTION VALUE="14">14</OPTION>
<OPTION VALUE="15">15</OPTION>
<OPTION VALUE="16">16</OPTION>
<OPTION VALUE="17">17</OPTION>
<OPTION VALUE="18">18</OPTION>
<OPTION VALUE="19">19</OPTION>
<OPTION VALUE="20">20</OPTION>
<OPTION VALUE="21">21</OPTION>
<OPTION VALUE="22">22</OPTION>
<OPTION VALUE="23">23</OPTION>
<OPTION VALUE="24">24</OPTION>
<OPTION VALUE="25">25</OPTION>
<OPTION VALUE="26">26</OPTION>
<OPTION VALUE="27">27</OPTION>
<OPTION VALUE="28">28</OPTION>
<OPTION VALUE="29">29</OPTION>
<OPTION VALUE="30">30</OPTION>
<OPTION VALUE="31">31</OPTION>
<OPTION VALUE="32">32</OPTION>
<OPTION VALUE="33">33</OPTION>
<OPTION VALUE="34">34</OPTION>
<OPTION VALUE="35">35</OPTION>
<OPTION VALUE="36">36</OPTION>
<OPTION VALUE="37">37</OPTION>
<OPTION VALUE="38">38</OPTION>
<OPTION VALUE="39">39</OPTION>
<OPTION VALUE="40">40</OPTION>
<OPTION VALUE="41">41</OPTION>
<OPTION VALUE="42">42</OPTION>
<OPTION VALUE="43">43</OPTION>
<OPTION VALUE="44">44</OPTION>
<OPTION VALUE="45">45</OPTION>
<OPTION VALUE="46">46</OPTION>
<OPTION VALUE="47">47</OPTION>
<OPTION VALUE="48">48</OPTION>
<OPTION VALUE="49">49</OPTION>
<OPTION VALUE="50">50</OPTION>
<OPTION VALUE="51">51</OPTION>
<OPTION VALUE="52">52</OPTION>
<OPTION VALUE="53">53</OPTION>
<OPTION VALUE="54">54</OPTION>
<OPTION VALUE="55">55</OPTION>
<OPTION VALUE="56">56</OPTION>
<OPTION VALUE="57">57</OPTION>
<OPTION VALUE="58">58</OPTION>
<OPTION VALUE="59">59</OPTION>
<OPTION VALUE="60">60</OPTION>
<OPTION VALUE="61">61</OPTION>
<OPTION VALUE="62">62</OPTION>
<OPTION VALUE="63">63</OPTION>
<OPTION VALUE="64">64</OPTION>
<OPTION VALUE="65">65</OPTION>
<OPTION VALUE="66">66</OPTION>
<OPTION VALUE="67">67</OPTION>
<OPTION VALUE="68">68</OPTION>
<OPTION VALUE="69">69</OPTION>
<OPTION VALUE="70">70</OPTION>
<OPTION VALUE="71">71</OPTION>
<OPTION VALUE="72">72</OPTION>
<OPTION VALUE="73">73</OPTION>
<OPTION VALUE="74">74</OPTION>
<OPTION VALUE="75">75</OPTION>
<OPTION VALUE="76">76</OPTION>
<OPTION VALUE="77">77</OPTION>
<OPTION VALUE="78">78</OPTION>
<OPTION VALUE="79">79</OPTION>
<OPTION VALUE="80">80</OPTION>
<OPTION VALUE="81">81</OPTION>
<OPTION VALUE="82">82</OPTION>
<OPTION VALUE="83">83</OPTION>
<OPTION VALUE="84">84</OPTION>
<OPTION VALUE="85">85</OPTION>
<OPTION VALUE="86">86</OPTION>
<OPTION VALUE="87">87</OPTION>
<OPTION VALUE="88">88</OPTION>
<OPTION VALUE="89">89</OPTION>
<OPTION VALUE="90">90</OPTION>
<OPTION VALUE="91">91</OPTION>
<OPTION VALUE="92">92</OPTION>
<OPTION VALUE="93">93</OPTION>
<OPTION VALUE="94">94</OPTION>
<OPTION VALUE="95">95</OPTION>
<OPTION VALUE="96">96</OPTION>
<OPTION VALUE="97">97</OPTION>
<OPTION VALUE="98">98</OPTION>
<OPTION VALUE="99">99</OPTION>
</SELECT>cm

 </P>
</form>
</BODY>

</HTML>
 
I'd consider using an input text rather than a combo with all values ...

Cheers,
Dian
 
Hi it seems to convert from feet/inches to centimeters correctly but not vice versa.

I'm using to justify the calculation.

I was having issues with teh remained aspect for centimeters to inches, so i created a work around, me thinks that their must be a call in JS that get the remainder of division calculation, but to no avail i found nothing.

Thanks
 
I think you should delete the entire section converting feet and inches to centimeters and start from scratch (I'd do the same with the metric to English conversion, too). You have your code overly complex there, and your lack of indenting makes it more difficult to read and troubleshoot.

Lee
 
Ok i redid the code, this is what i hvae, which works now, buts its still ugly, any pointers to clean it up, thanks

Code:
function heightChg(feet,inch,cm1,cm2,daVal)
{
var temp1=0;
var temp2=0;
var temp3="";
var temp4="";

  if (daVal==1){
	temp1 = inch/12;
	temp2= ((feet+temp1)*3.048)+"";
	temp3 = temp2.split(".",2);
	document.getElementById('cm1').value = temp3[0];
	document.getElementById('cm2').value = temp3[1];	
  } else if (daVal==2){

	temp1 = ((cm1*1)+(cm2*1/100))*0.0328083989501312;  //eg 130.48cm
	temp2 = parseInt(temp1);  //eg: 56.53845 inches
	document.getElementById('feet').value=temp2;

    temp4 = temp1+"";
	temp3 = temp4.split(".",2);
	temp4 = temp3[1].slice(0,2);
	temp1=temp4.charAt(0);
	temp2=temp4.charAt(1);
	temp1=temp1+"."+temp2;
	temp1=temp1*1;
	temp1 = Math.round(temp1);
    document.getElementById('inches').value=temp1;

  }
}
 
I was thinking something like:
Code:
if (daVal==1){

  var cm = Math.round(((feet * 12) + (inch * 1)) * 254);
  cm1 = parseInt(cm / 100, 10);
  cm2 = cm % 100;
  document.getElementById('cm1').value = cm1;
  document.getElementById('cm2').value = cm2;    

  }else if (daVal==2){

  inch = Math.round(((cm1*100)+(cm2*1)) / 254);
  feet = inch / 12;
  inch %= 12;
  document.getElementById('feet').value=feet;
  document.getElementById('inches').value=inch;
  }

Lee
 
Your original will also have a problem when the answer to your calculation isn't one of the values in the option list. For example, 40cm has no equivalent in the feet and inches section.

This is my simplified version using a span for the answer:

<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">

function ImperialToMetric()
{
feet = parseInt(document.getElementById('feet').value);
inches = parseInt(document.getElementById('inches').value);
cm = (feet * 12 + inches) * 2.54;
document.getElementById('metric').innerHTML = " = " + Math.round(cm) + " cm";
}

</script>

</HEAD>

<BODY>
<form id="f1">

<SELECT SIZE="1" id="feet" NAME="feet" onchange="ImperialToMetric()">
<OPTION VALUE="3">3</OPTION>
<OPTION VALUE="4">4</OPTION>
<OPTION selected VALUE="5">5</OPTION>
<OPTION VALUE="6">6</OPTION>
<OPTION VALUE="7">7</OPTION>
<OPTION VALUE="8">8</OPTION>
</SELECT>feet&nbsp;&nbsp;&nbsp;
<SELECT SIZE="1" NAME="inches" id="inches" onchange="ImperialToMetric()">
<OPTION selected VALUE="0" selected>0</OPTION>
<OPTION VALUE="1">1</OPTION>
<OPTION VALUE="2">2</OPTION>
<OPTION VALUE="3">3</OPTION>
<OPTION VALUE="4">4</OPTION>
<OPTION VALUE="5">5</OPTION>
<OPTION VALUE="6">6</OPTION>
<OPTION VALUE="7">7</OPTION>
<OPTION VALUE="8">8</OPTION>
<OPTION VALUE="9">9</OPTION>
<OPTION VALUE="10">10</OPTION>
<OPTION VALUE="11">11</OPTION>
</SELECT>inch

<span id="metric"></span>

</form>
</BODY>

</HTML>
 
thanks physicsGuy, will be useful for other, in my case i'm dealing with adults. cheers

Lee that looks very clean, i will try it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top