Problem with SUM function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • viki1967
    Contributor
    • Oct 2007
    • 263

    Problem with SUM function

    Good afternoon to everyone.

    I write this page htm with JavaScript code:

    Code:
    <script language="javascript" type="text/javascript">
    <!--
    
    function parseNumber(val)
    {
      val = parseFloat(val);
      if (isNaN(val)) return(0.0);
     
      return(val);
    }
    
    var idList = [630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646];
    
    function calcola(frmObj, id)
    {
                                               
      var sum1 = 0.0, sum2 = 0.0;
     
      for ( var n = 0 ; n < idList.length ; n++ )
        {
          sum1 += parseNumber(frmObj["BANCO-" + idList[n]].value);
          sum2 += parseNumber(frmObj["TAVOLI-" + idList[n]].value);
        }
     
      frmObj["TOT_BANCO-" + idList[0]].value = sum1.toFixed(2);
      frmObj["TOT_TAVOLI-" + idList[0]].value = sum2.toFixed(2);
    }
    
    // -->
    </script>		
    
          <td><input type="text" name="BANCO-630" size="5" value="33,18" onkeyup="calcola(this.form, 630)"></td>    
          <td><input type="text" name="TAVOLI-630" size="5" value="128,73" onkeyup="calcola(this.form, 630)"></td> 
          <td><input type="text" name="TOT_BANCO-630" size="5" value="902,63" readonly></td>
          <td><input type="text" name="TOT_TAVOLI-630" size="5" value="1961,92" readonly></td>
    
          <td><input type="text" name="BANCO-631" size="5" value="0" onkeyup="calcola(this.form, 631)"></td>    
          <td><input type="text" name="TAVOLI-631" size="5" value="6,5" onkeyup="calcola(this.form, 631)"></td> 
          <td><input type="text" name="TOT_BANCO-631" size="5" value="0" readonly></td>
          <td><input type="text" name="TOT_TAVOLI-631" size="5" value="0" readonly></td>
    The script works in this way if updated quantity :

    TOT_BANCO-630 = BANCO-630 +/- BANCO-631
    TOT_TAVOLI-630 = TAVOLI-630 +/- TAVOLI-631

    This script not return errors but the new sum are wrong.

    Link of the page:


    Help me please.
    Viki
  • viki1967
    Contributor
    • Oct 2007
    • 263

    #2
    [Javascript] Function Calcola

    Hi everyone.

    I have problem whit this function:

    Code:
    function calcola(frmObj, id)
    {
                                               
      var sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
     
      for ( var n = 0 ; n < idList.length ; n++ )
      
        {
        
          sum1 += parseNumber(frmObj["BANCO-" + idList[n]].value);
          sum2 += parseNumber(frmObj["TAVOLI-" + idList[n]].value);
                
          sum3 += parseNumber(frmObj["TOT_BANCO-" + idList[n]].value) + 
                  parseNumber(frmObj["TOT_TAVOLI-" + idList[n]].value);          
                                           
        }
     
      frmObj["TOT_BANCO-" + idList[0]].value = sum1.toFixed(2);
      frmObj["TOT_TAVOLI-" + idList[0]].value = sum2.toFixed(2);
      frmObj["TOT_SENZA_IVA-" + idList[0]].value = sum3.toFixed(2);
       
      var vl = frmObj["TOT_CON_IVA-" + idList[0]].value = sum3.toFixed(2) * 0.1 +
                                                          parseNumber(frmObj["TOT_SENZA_IVA-" + idList[0]].value);
    
      frmObj["TOT_CON_IVA-" + idList[0]].value = vl.toFixed(3);                                                      
    
    
    }
    The link of the page web:


    Try change values line number 4:

    BANCO = 3
    TAVOLI = 3
    TOTALE SENZA IVA = 1165.37 ???? its wrong

    BANCO = 3.3
    TAVOLI = 3.3
    TOTALE SENZA IVA = 1155.40 !!!! its OK

    Was going on ?

    Can you help me?

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      You're only setting the total for 630. Do you need to update for all totals?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Since this is related somewhat to the first problem, I've merged the two threads.

        Comment

        • viki1967
          Contributor
          • Oct 2007
          • 263

          #5
          Originally posted by acoder
          You're only setting the total for 630. Do you need to update for all totals?

          Yes, I don't have it any problem, thanks:

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Does that mean it's working now?

            If not, the figures that you mention don't match with the default values.

            Comment

            • viki1967
              Contributor
              • Oct 2007
              • 263

              #7
              Can you give me a example please?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                I meant this:
                Originally posted by viki1967
                Try change values line number 4:

                BANCO = 3
                TAVOLI = 3
                TOTALE SENZA IVA = 1165.37 ???? its wrong

                BANCO = 3.3
                TAVOLI = 3.3
                TOTALE SENZA IVA = 1155.40 !!!! its OK
                The figures are something else, so I can't be sure what's right or what's not unless I manually sum the fields.

                Comment

                • viki1967
                  Contributor
                  • Oct 2007
                  • 263

                  #9
                  Originally posted by acoder
                  I meant this:

                  The figures are something else, so I can't be sure what's right or what's not unless I manually sum the fields.
                  Line 4:

                  BANCO = 3
                  TAVOLI = 0
                  TOTALE SENZA IVA = 2801.8

                  BANCO = 3.3 ( +0.3 )
                  TAVOLI = 3.3 ( +3.3 )
                  TOTALE SENZA IVA = 2805.4

                  2801.8 + 0.3 + 3.3 = 2805.4

                  It's OK ?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    That looks about right, so it's working. What changes did you make?

                    Comment

                    • viki1967
                      Contributor
                      • Oct 2007
                      • 263

                      #11
                      The lines 19 and 20 :

                      Code:
                      function calcola(frmObj, id)
                      {
                                                                 
                        var sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
                       
                        for ( var n = 0 ; n < idList.length ; n++ )
                        
                          {
                          
                            sum1 += parseNumber(frmObj["BANCO-" + idList[n]].value);
                            sum2 += parseNumber(frmObj["TAVOLI-" + idList[n]].value);
                                  
                              
                            
                          }
                        frmObj["TOT_BANCO-" + idList[0]].value = sum1.toFixed(2);
                        frmObj["TOT_TAVOLI-" + idList[0]].value = sum2.toFixed(2);
                      
                        [i]sum3 = sum1 + sum2;[/i]    
                        [i]frmObj["TOT_SENZA_IVA-" + idList[0]].value = sum3.toFixed(2);[/i]
                      
                      
                        var vl = frmObj["TOT_CON_IVA-" + idList[0]].value = sum3.toFixed(2) * 0.1 +
                                                                            parseNumber(frmObj["TOT_SENZA_IVA-" + idList[0]].value);
                      
                        frmObj["TOT_CON_IVA-" + idList[0]].value = vl.toFixed(3);                                                      
                      
                      
                      }
                      Replace this:

                      Code:
                      sum3 += parseNumber(frmObj["TOT_BANCO-" + idList[n]].value) + 
                              parseNumber(frmObj["TOT_TAVOLI-" + idList[n]].value);
                      whit:

                      Code:
                        sum3 = sum1 + sum2;    
                      frmObj["TOT_SENZA_IVA-" + idList[0]].value = sum3.toFixed(2);

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Thanks for posting and glad it's working!

                        Comment

                        • viki1967
                          Contributor
                          • Oct 2007
                          • 263

                          #13
                          You are welcome and I'am very happy to found this Community!

                          Comment

                          Working...