Skip to content

Simple

Conversions

This script provide functions for a lot of useful conversions.

This script needs an HTML form on the page called "formMain" that has 2 input fields ("txtInput" and "txtOutput") as well as a button to call each function.

// Conversion functions

function cmdConvertLux(){
    document.formMain.txtOutput.value = (Math.round((document.formMain.txtInput.value * .093)*10000)/10000) + " Lux";
}

function cmdConvertFt(){
    document.formMain.txtOutput.value = (Math.round((document.formMain.txtInput.value * 10.76)*10000)/10000) + " Footcandles";
}

function cmdF2C(){
    document.formMain.txtOutput.value = (Math.round(((5/9)*(document.formMain.txtInput.value-32))*10000)/10000) + " Degrees Celsius";
}

function cmdC2F(){
    document.formMain.txtOutput.value = (Math.round((((9/5)*document.formMain.txtInput.value)+32)*10000)/10000) + " Degrees Fahrenheit";
}

function cmdFeet2Meters(){
    document.formMain.txtOutput.value = (Math.round((.3048 * document.formMain.txtInput.value)*10000)/10000) + " Meters";
}

function cmdMeters2Feet(){
    document.formMain.txtOutput.value = (Math.round((3.280839895 * document.formMain.txtInput.value)*10000)/10000) + " Feet";
}

function cmdLbs2Kgs(){
    document.formMain.txtOutput.value = (Math.round((.45359237 * document.formMain.txtInput.value)*10000)/10000) + " Kilograms";
}

function cmdKgs2Lbs(){
    document.formMain.txtOutput.value = (Math.round((2.204622622 * document.formMain.txtInput.value)*10000)/10000) + " Pounds";
}

function cmdY2G(){
    document.formMain.txtOutput.value = (Math.round((3.8462 * document.formMain.txtInput.value)*10000)/10000) + " Liters";
}

function cmdG2Y(){
    document.formMain.txtOutput.value = (Math.round((.26 * document.formMain.txtInput.value)*10000)/10000) + " Gallons";
}

function cmdInches2Milli(){
    document.formMain.txtOutput.value = (Math.round((25.4 * document.formMain.txtInput.value)*10000)/10000) + " Millimeters";
}

function cmdMilli2Inches(){
    document.formMain.txtOutput.value = (Math.round((.03937007874 * document.formMain.txtInput.value)*10000)/10000) + " Inches";
}

function cmdKnots2MPH(){
    document.formMain.txtOutput.value = (Math.round((1.15077945 * document.formMain.txtInput.value)*10000)/10000) + " Miles per Hour";
}

function cmdMPH2Knots(){
    document.formMain.txtOutput.value = (Math.round((0.8689762404081859 * document.formMain.txtInput.value)*10000)/10000) + " Knots";
}

function cmdD2B(){
    var Bin = "";
    var Num = Math.round(document.formMain.txtInput.value);
    var I = 1;

    while (I < Num){
        I = I * 2;
    }

    if (I != Num) {
        I = I / 2;
    }

    while (I > .5){
        if (Num - I < 0) {  
            Bin = Bin + "0";
        } else {
            Bin = Bin + "1";
            Num = Num - I   ;
        }
    I = I / 2;
    }

    while (Bin.length < 9){
        Bin = "0" + Bin;
    }

    document.formMain.txtOutput.value = Bin + " in Binary";
}

function cmdB2D(){
    var phil = document.formMain.txtInput.value;
    var J = (phil.length);
    var steve = 0;
    var K = 0;

    while (K <= (phil.length)-1) {
        if (phil.substring(K, K+1) == "1") {
            steve = steve + Math.pow(2, J-1);
        }   
        J = J - 1;
        K = K + 1;
    } 

    document.formMain.txtOutput.value = steve + " in Decimal";

}
Back to top