not getting exact answer when adding floating numbers.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shiniskumar
    New Member
    • Feb 2007
    • 58

    not getting exact answer when adding floating numbers.

    Ive got to sum up 5 numbers.
    125000+23500.75 +5525725.79+325 7500+63542.36
    When summing these i should get the answer as 8995268.9
    but im getting the answer as 8995268.899999

    My script code is as follows
    ------------------------------------------
    var totalDebit =0;
    (here dtAmt[i] contains each of the above specified numbers)
    for(i=0;i<5;i++ ){
    if(!isEmpty(dtA mt[i].value)){
    totalDebit =parseFloat(tot alDebit) +parseFloat(dtA mt[i].value);
    }
    }

    Y do i get the anonymous answer.?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shiniskumar
    Ive got to sum up 5 numbers.
    125000+23500.75 +5525725.79+325 7500+63542.36
    When summing these i should get the answer as 8995268.9
    but im getting the answer as 8995268.899999

    My script code is as follows
    ------------------------------------------
    var totalDebit =0;
    (here dtAmt[i] contains each of the above specified numbers)
    for(i=0;i<5;i++ ){
    if(!isEmpty(dtA mt[i].value)){
    totalDebit =parseFloat(tot alDebit) +parseFloat(dtA mt[i].value);
    }
    }

    Y do i get the anonymous answer.?
    I will move this to the Javascript forum where it belongs and where you are likey to be told about how parseFloat approximates values ...

    Comment

    • dorinbogdan
      Recognized Expert Contributor
      • Feb 2007
      • 839

      #3
      After calcualtion add the following formatting (to get 2 decimals):
      Code:
      totalDebit = totalDebit.toFixed(2);
      It seem that only works in IE 5.5+ and Netscape 6+.

      Comment

      • dorinbogdan
        Recognized Expert Contributor
        • Feb 2007
        • 839

        #4
        Or, try this link for better approach and cross-browser support.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by shiniskumar
          Ive got to sum up 5 numbers.
          125000+23500.75 +5525725.79+325 7500+63542.36
          When summing these i should get the answer as 8995268.9
          but im getting the answer as 8995268.899999

          My script code is as follows
          ------------------------------------------
          var totalDebit =0;
          (here dtAmt[i] contains each of the above specified numbers)
          for(i=0;i<5;i++ ){
          if(!isEmpty(dtA mt[i].value)){
          totalDebit =parseFloat(tot alDebit) +parseFloat(dtA mt[i].value);
          }
          }

          Y do i get the anonymous answer.?
          Use
          Code:
          totalDebit.toFixed(1);
          to round to one decimal place and yes, parseFloat is only approximate.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Ah, so this was double posted!

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Threads merged.

              Comment

              • shiniskumar
                New Member
                • Feb 2007
                • 58

                #8
                not getting exact answer when adding floating numbers.

                when i add 1125345.55+1056 87.69+178954.89 +86933.72

                i should get the answer as 2172811.85 but i get the answer as 2172811.84999

                the code is as
                where dtAmt[i] consists of the numbers

                var floatSum=0;
                var intSum=0;
                for(i=0;i<openi ngLedger1.lengt h;i++){

                if(!isEmpty(dtA mt[i].value)){
                var amnt=dtAmt[i].value;
                if(!isInteger(d tAmt[i].value)){
                floatSum=parseF loat(floatSum)+ parseFloat(amnt );
                }
                else{
                intSum=parseFlo at(intSum)+pars eFloat(amnt);
                }
                totalDebit =parseFloat(flo atSum) +parseFloat(int Sum);
                }

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  See this page.

                  Comment

                  Working...