Forum Moderators: open

Message Too Old, No Replies

Need some calculator help

need to show 2 decimal places in output

         

tonynoriega

4:57 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im new to javascript, so i can quite figure this out...
how can i show two decimal places in my output?

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>

Fotiman

8:04 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Some things to note...

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).

tonynoriega

8:49 pm on Sep 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Excellent...works perfectly.

Thanks alot....

RonPK

8:23 am on Sep 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JavaScript 1.5 introduced
number.toFixed( [digits] )
. Supported by all recent browsers and IE 6.

var a = 0.145; 
alert(a.toFixed(2));

Will show 0.15.

var a = 5; 
alert(a.toFixed(2));

Will show 5.00.

Fotiman

1:49 pm on Sep 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, toFixed is buggy in IE5.5 (or so I've read).