Convert Length Measurements
Imperial to Metric
by akmiecik
HTML
<table>
<tbody>
<tr>
<td>
<input id="inpImperial" type="number" />
</td>
<th>
<select id="selImperial"></select>
</th>
</tr>
<tr>
<th colspan="2" style="text-align: center;">
<div>
<button id="btnConversion">Imperial to Metric</button>
</div>
</th>
</tr>
<tr>
<td>
<input id="inpMetric" type="number" />
</td>
<th>
<select id="selMetric"></select>
</th>
</tr>
</tbody>
</table>
CSS
table { width: 100%; }
td { text-align: right; }
th { text-align: left; }
input[type=text], input[type=number] { width: 50%; }
JavaScript
function meters_t () {
var metrics = convertImperialMetric.metrics,
imperials = convertImperialMetric.imperials,
args = arguments,
conversionTypes = { imperial: 'imperial', metric: 'metric' },
toFixed = false, toFixedX = 2,
intX, typImp, typMet, conType = 'metric',
$ret;
conversionTypes.i = conversionTypes.imp = conversionTypes.imperial;
conversionTypes.m = conversionTypes.met = conversionTypes.metric;
function setVarz(c) {
for (i in c) {
var a = c[i];
switch (typeof a) {
case "boolean":
toFixed = a;
break;
case "number":
void 0 == intX ? intX = a : toFixedX = a;
break;
case "string":
isNaN(parseFloat(a)) || void 0 != intX ? imperials.hasOwnProperty(a) ? typImp = a : metrics.hasOwnProperty(a) ? typMet = a : conversionTypes.hasOwnProperty(a) && (conType = conversionTypes[a]) : intX = parseFloat(a);
break;
case "object":
if (a instanceof Array) setVarz.apply(this, [a]);
else if (a instanceof Object)
for (h in a) {
var b = a[h];
conversionTypes.hasOwnProperty(h) ? conType = conversionTypes[h] : imperials.hasOwnProperty(h) ? (typImp =
h, void 0 != intX || isNaN(parseFloat(b)) || (intX = parseFloat(b))) : metrics.hasOwnProperty(h) ? (typMet = h, void 0 != intX || isNaN(parseFloat(b)) || (intX = parseFloat(b))) : setVarz.apply(this, [
[b]
])
}
}
}
};
setVarz(args);
if (!isNaN(parseFloat(intX)) && imperials.hasOwnProperty(typImp) && metrics.hasOwnProperty(typMet) && conversionTypes.hasOwnProperty(conType)) {
if (conType == 'metric') {
var inches = intX * imperials[typImp],
centimeters = inches * 2.54;
$ret = centimeters * metrics[typMet];
}
else if (conType == 'imperial') {
var centimeters = intX / metrics[typMet],
inches = centimeters / 2.54;
$ret = inches / imperials[typImp];
}
}
return toFixed ? parseFloat($ret.toFixed(toFixedX)) : $ret;
}
convertImperialMetric.imperials = {
inches: 1,
feet:...