Unit Converter

by abehnaz

HTML

<input id="input" type="text" value=104></input>
<button id="btn">Convert Me!</button>

JavaScript

//When button is clicked, call convert handler
$("#btn").click(function() {
  compute($("#input").val());
});

//From Muzique
function compute (obj) {
    var v3=eval(obj.X2.value.charAt(2));
    var v1=eval(obj.X2.value);
    var v4=eval(obj.X2.value.length);
    
    if (v4==3) { 
        v1=Math.floor(v1/10);
        v3=Math.pow(10,v3);
        
        obj.Out.value=v1*v3;
        obj.X1.value=(v1*v3)/1000000;
        obj.X3.value=(v1*v3)/1000;
    }
    else
    { 
        v3=1;
        obj.Out.value=v1*v3;
        obj.X1.value=(v1*v3)/1000000;
        obj.X3.value=(v1*v3)/1000;
    }
}

//Strip digits and return the answer
function convert(num){
    var valueDigits = "00";
    var exponentDigit = "0";
    var finalValue = "";
    //Parse the value digits
    valueDigits = num.slice(0,2);
    valueDigits = parseInt(valueDigits);
    //Parse the exponent digit
    exponentDigit = num.slice(2,3);
    exponentDigit = parseInt(exponentDigit);
    exponentDigit = Math.pow(10,exponentDigit);
    //Perform the calculation (with 10^-12 factor for caps)
    finalValue = valueDigits * exponentDigit * Math.pow(10,-12);
    //Convert to engineering units & add ohm symbol
    finalValue = engineeringUnits(finalValue);
    
    alert(finalValue);
}

//Convert a value to its engineering format equivalent
function engineeringUnits(value){
    abbr="";
    abs = Math.abs(value);
    if (abs >= Math.pow(10, 6)) {
        // mega
        abbr = " M";
        value = value / Math.pow(10, 6);
    } else if (abs < Math.pow(10, 6) && abs >= Math.pow(10, 3)) {
        // kilo
        abbr = " K";
        value = value / Math.pow(10, 3);
    } else if(abs <= Math.pow(10, -3) && abs > Math.pow(10, -6)) {
        // milli
        abbr = " m";
        value = value * Math.pow(10, 3);
    } else if(abs <= Math.pow(10, -6) && abs > Math.pow(10, -9)) {
        // micro
        abbr = " u";
        value = value * Math.pow(10, 6);
    } else if(abs <= Math.pow(10, -9) && abs > Math.pow(10,...