Forum Moderators: open
hence: savings of: $5.00 instead of $5.5
********************************************************************
function compute(form) {
var invPerWk = form.invPerWk.value;
var postage = form.invPerWk.value * .39;
var printing = form.invPerWk.value * .06;
var materials = form.invPerWk.value * .05;
var laborHrs = form.laborHrs.value;
var costPerLabor = form.costPerLabor.value;
var total1 = laborHrs * costPerLabor;
var total2 = ((postage + printing) + materials);
var total3 = ((total1 + total2) * 4);
var total4 = total3 * 12;
var total5 = ((costPerLabor / 2) * 4);
var total6 = ((costPerLabor / 2) * 12);
var total7 = total3 - total5;
var total8 = total4 - total6;
form.moCost.value = total3;
form.yrCost.value = total4;
form.moCostPDF.value = total5;
form.yrCostPDF.value = total6;
form.moSavings.value = total7;
form.yrSavings.value = total8;
}
function ClearForm(form) {
form.invPerWk.value = "";
form.postage.value = "";
form.printing.value = "";
form.materials.value = "";
form.laborHrs.value = "";
form.costPerLabor.value = "";
form.moCost.value = "";
form.yrCost.value = "";
form.moCostPDF.value = "";
form.yrCostPDF.value = "";
form.moSavings.value = "";
form.yrSavings.value = "";
form.save.value = "";
}
</script>
First, doing math with decimals (floats) is not always reliable. That is, there may be some rounding errors. If you're ok with that then...
Add a new method to the Number prototype to convert the value to "n" number of decimal places (note, this function was written by someone else):
Number.prototype.toDecimals=function(n){
n=(isNaN(n))? 2: n;
var nT=Math.pow(10,n);
function pad(s){
s=s¦¦'.';
return (s.length>n)? s: pad(s+'0');
}
return (isNaN(this))? this: (new String(
Math.round(this*nT)/nT
)).replace(/(\.\d*)?$/,pad);
}
Next, you should be doing additional validation on the form fields to verify that the values are numeric. Also, you should call parseFloat() on the value to get the numeric value.
function compute(form) {
// Get the numeric values
var invPerWk = parseFloat(form.invPerWk.value);
var laborHrs = parseFloat(form.laborHrs.value);
var costPerLabor = parseFloat(form.costPerLabor.value);
// Validate that numbers were entered
if( isNaN(invPerWk) ¦¦ isNaN(laborHrs) ¦¦ isNaN(costPerLabor) ) return;
// Validation passed, calculate some values
var postage = invPerWk * .39;
var printing = invPerWk * .06;
var materials = invPerWk * .05;
// Calculate the totals
var total1 = laborHrs * costPerLabor;
var total2 = ((postage + printing) + materials);
var total3 = ((total1 + total2) * 4);
var total4 = total3 * 12;
var total5 = ((costPerLabor / 2) * 4);
var total6 = ((costPerLabor / 2) * 12);
var total7 = total3 - total5;
var total8 = total4 - total6;
// Write the values back to the form, showing 2 decimals
form.moCost.value = total3.toDecimals(2);
form.yrCost.value = total4.toDecimals(2);
form.moCostPDF.value = total5.toDecimals(2);
form.yrCostPDF.value = total6.toDecimals(2);
form.moSavings.value = total7.toDecimals(2);
form.yrSavings.value = total8.toDecimals(2);
}
Note, be sure to replace the ¦¦ characters with pipe characters (this forum replaces them).