...

# Precision issues

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

Choopernickel
12-22-2003, 10:14 PM
Wouldn't you know it, I found the Number.toPrecision() method. It seems to be doing the trick -- though figuring out how precise to be (how many significant digits) is a bit of a kluge:

parseFloat(currentVal,10).toPrecision(Math.round(parseInt(currentVal,10)).toString().length + 1);

I guess I could wrap that all up in a function or method, but this works for now.

Thanks anyway!