Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: conversion of imperial to metric (feet to centimeters - persons height)

1. ## conversion of imperial to metric (feet to centimeters - persons height)

Hi I've been trying to write a function that can change the users input from feet/inches to centimeters and vice versa, i can't seem to nail it down, and the more i go at it the unglier the code is getting, was wondering if a skilled someone can help me out, this is what i have,
so far i converts feet/inches to centimeters correctly (atleast i think it does) converting centimeters back to feet/inches is really getting me, any ideas... thanks

Code:
```<HTML>
<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;
temp2 = (feet*12)*2.54;
var feet2cm = temp1+temp2;
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.slice(0,2))/100)*12;
temp1=(temp2-parseInt(temp2))*10;
temp1=parseInt(temp1);
document.getElementById('inches').value=temp1;
}
}

</script>

<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>```

• * bUMP *

• here is a simple solution i came up with to this exact problem.
this should convert most popular units.

at first i had made one big function with a million if/thens.
then i got it down to using json to store a table of conversions.
then i found a way to replace the table with just one row of data.

it is faster than other converters ive seen because the conversion formulas don't have to be evaluted each time, or even once.
this reduces precision slightly, so don't fly a plane on it, but this should work for most things (look at all the decimals).

Code:
```(function (){ //a package of the conversion unit object and the Number length-converter prototype

var LengthOb=({inch:12, inches:12, foot:1, feet:1, yard:0.3333333333333333, mile:0.00018938676565281618, miles:0.00018938676565281618, fathom:0.16666666666666666, nauticalMile:0.868421, nanometer:30479999.02464003, micron:304799.9902464003, micrometer:304799.9902464003, millimeter:304.7999902464003, centimeters:30.47999902464003, centimeter:30.47999902464003, meter:0.3047999902464003, kilometer:0.0003047999902464003});

Number.prototype.lengths=function (s, e) {    return this * LengthOb[e] / LengthOb[s];  }

//example usage: (number).lengths("starting unit" , "destination unit"

var myHeight= 75;

var toLA= 1275;
i will leave the html making up to you, but now you can finally convert microns to fathoms!

-EDIT -
i noticed that you will also have to add (inches /12) to feet (or vice-versa) before using my converter...

• But it doesn't convert chains or furlongs!

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•