Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Precision issues

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

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

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •