Choopernickel

12-22-2003, 10:01 PM

I know that math with Javascript isn't always the prettiest thing, but this wierdness with precision is causing me to join the ranks of bald men the world over.

How can I guarantee a rounding to one decimal in an addition process wherein I add several single-decimal-precision numbers? I keep getting values that are... close, but that's not quite cutting it.

In the snippet below (pulled from a function), this handles the addition of values in my form. The assignment of the sums to the proper form elements, all of which follow the naming guidelines laid out in the script, is handled just after. Bear this in mind: legCount starts at 2, then is manipulated and will stay in the range 0...*

var i = legCount;

var totalsFields = new Array("adjTime","waitTime","ppTime","passCount","nautMiles","passMiles","landExp","parkExp","hangExp","fuelExp","othExp");

var j = totalsFields.length;

do {

// prepopulate currTotals with zeros

currTotals[totalsFields[--j]] = parseFloat(0,10);

} while (j)

do {

j = totalsFields.length;

do {

ix = totalsFields[--j];

currentVal = theForm.elements["leg" + i + ix].value;

if (currentVal.length && currentVal == parseFloat(currentVal,10)) {

currTotals[ix] += Math.round(parseFloat(currentVal,10)*10)/10;

}

} while (j)

} while (--i > 0);

No matter how many times I Math.round(x*10)/10, it always ends up with the ridiculous unrounded precision.

I know this isn't one for the newbies; I'm hoping the serious script jocks can point something out to me.

TIA.

How can I guarantee a rounding to one decimal in an addition process wherein I add several single-decimal-precision numbers? I keep getting values that are... close, but that's not quite cutting it.

In the snippet below (pulled from a function), this handles the addition of values in my form. The assignment of the sums to the proper form elements, all of which follow the naming guidelines laid out in the script, is handled just after. Bear this in mind: legCount starts at 2, then is manipulated and will stay in the range 0...*

var i = legCount;

var totalsFields = new Array("adjTime","waitTime","ppTime","passCount","nautMiles","passMiles","landExp","parkExp","hangExp","fuelExp","othExp");

var j = totalsFields.length;

do {

// prepopulate currTotals with zeros

currTotals[totalsFields[--j]] = parseFloat(0,10);

} while (j)

do {

j = totalsFields.length;

do {

ix = totalsFields[--j];

currentVal = theForm.elements["leg" + i + ix].value;

if (currentVal.length && currentVal == parseFloat(currentVal,10)) {

currTotals[ix] += Math.round(parseFloat(currentVal,10)*10)/10;

}

} while (j)

} while (--i > 0);

No matter how many times I Math.round(x*10)/10, it always ends up with the ridiculous unrounded precision.

I know this isn't one for the newbies; I'm hoping the serious script jocks can point something out to me.

TIA.