function perRound(num, precision) {
	var precision = 3; //default value if not passed from caller, change if desired
	// remark if passed from caller
	precision = parseInt(precision); // make certain the decimal precision is an integer
    var result1 = num * Math.pow(10, precision);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, precision);
    return zerosPad(result3, precision);
}

function zerosPad(rndVal, decPlaces) {
    var valStrg = rndVal.toString(); // Convert the number to a string
    var decLoc = valStrg.indexOf("."); // Locate the decimal point
    // check for a decimal 
    if (decLoc == -1) {
        decPartLen = 0; // If no decimal, then all decimal places will be padded with 0s
        // If decPlaces is greater than zero, add a decimal point
        valStrg += decPlaces > 0 ? "." : "";
    }
    else {
        decPartLen = valStrg.length - decLoc - 1; // If there is a decimal already, only the needed decimal places will be padded with 0s
    }
     var totalPad = decPlaces - decPartLen;    // Calculate the number of decimal places that need to be padded with 0s
    if (totalPad > 0) {
        // Pad the string with 0s
        for (var cntrVal = 1; cntrVal <= totalPad; cntrVal++) 
            valStrg += "0";
        }
    return valStrg;
}
// send the value in as "num" in a variable

// clears field of default value
function clear_field(field) {
		if (field.value==field.defaultValue) {
			field.value=''
		}
	}

function compute (obj) {

with (Math) {

z1=eval(obj.diz.value);
z2=eval(obj.doz.value);
a=eval(obj.dbin.value);
k=Math.pow(10, (a/20));

r1=Math.round(z1*(((k*k)+1)/((k*k)-1))-2*(Math.sqrt(z1*z2))*(k/((k*k)-1)));
r2=Math.round(z2*(((k*k)+1)/((k*k)-1))-2*(Math.sqrt(z1*z2))*(k/((k*k)-1)));
r3=Math.round((2*(sqrt(z1*z2))*(k/((k*k)-1))));
r4=Math.round(0.5*r1);
r5=Math.round(0.5*r2);

if (r2<"0") {
    alert("Negative resistance values indicate an illegal impedance matching and dbin combination. The values are beyond the scope of this calculator.")
}

obj.tio.value=r1;
obj.too.value=r2;
obj.tbplo.value=r3;
obj.hbplo.value=r3;
obj.hipo.value=r4;
obj.hiso.value=r4
obj.hopo.value=r5;
obj.hoso.value=r5;
obj.tiz.value=z1;
obj.hiz.value=z1
obj.toz.value=z2;
obj.hoz.value=z2;

 }	
}
