4 Replies Latest reply on Apr 23, 2010 6:40 AM by Working for a living

    Calculated sum requires fields to be mutliplied by 1

    Working for a living

      Hi All:

       

      I am hoping someone can explain why this happens.  I might be scripting this wrong but can not find a reason why this happens.

       

      Scenerio:

       

      I have a column of fields that need to be summed automatically as the client enters numerical data. The client can enter data into any of these fields.  When a field is skipped (I.E. the end user has no value to enter into this field), the calculation goes way off

       

      For example:

      enter 1000

      1000

      1000 and the total is 3000

      Skip field

      Skip field

      enter 5000

      the total goes from 3000 to 30,005,000

       

      Custom calculation script is:

      var a = this.getField("PartE1").value;
      var b = this.getField("PartE2").value;
      var c = this.getField("PartE3").value;
      var d = this.getField("PartE4").value;
      var e = this.getField("PartE5").value;
      var f = this.getField("PartE6").value;
      var g = this.getField("PartE7").value;
      var h = this.getField("PartE8").value;
      var i = this.getField("PartE9").value;
      var j = this.getField("PartE10").value;
      var k = this.getField("PartE11").value;
      var l = this.getField("PartE12").value;
      var m = this.getField("PartE13").value;

      event.value = (a + b + c + d + e + f + g + h + i + j + k + l + m);

      if (event.value<0) event.value=0;

      // if the result is zero force the value to an empty string
      if (event.value == 0) event.value = "";

       

      if the custom script is changed to

      event.value = (a*1  + b*1 + c*1 + d*1 + e*1 + f *1 + g*1 + h*1 + i*1 + j*1 + k*1 + l*1 + m*1);

      The fields total correctly.

       

      I am assuming that in the first instance the null values in between the second values, cause the 2 totals to be concatenated.  When the fields are multiplied by 1 it creates a 0 value for the fields and therefore the sum works.

       

      Is this correct?  Is there a way around this so we would not have to use the "*1" for everyfield?

       

      Thank you,

       

      PS I have a sample file but dont know how to attach it to this forum.

        • 1. Re: Calculated sum requires fields to be mutliplied by 1
          try67 MVP & Adobe Community Professional

          If it works, then I guess it's correct.

          However, you can replace all of that code with this:

           

          total = 0;
          for (i=1; i<=13; i++) {

              val = this.getField("PartE"+i).value;
              if (val!=null && val!="")
                  total += Number(val);
          }
          if (total<=0) {
              event.value = "";
          } else event.value = total;

           

          Message was edited by: try67 Edit: adjusted the code for empty fields

          • 2. Re: Calculated sum requires fields to be multiplied by 1
            gkaiseril MVP & Adobe Community Professional

            JavaScript is not very strict about typing string and numeric values and tries to use the value type it thinks is the correct one, but it is usually wrong, so one needs to force the value to numeric by using an operator that only works with numeric data or use the 'Number()' constrictor.

             

            The '+' operator is the string concatenation and arithmetic addition operator. But the "*", mulitiplication, or "/", dividion, operators ony work with numeric fields.

            • 3. Re: Calculated sum requires fields to be mutliplied by 1
              MarkWalsh Level 4

              If all you want is a simple summary field, you're probably better off using the Sum function under the calculation tab. So much less hassle, plus it's easier to maintain. I try to not use Javascript for the simpler functions that Acrobat can do itself efficiently. If I want to then suppress a zero result, I'll just use the javascript to format the final result.

               

              Plus, if you changed your naming convention from "PartE1", "PartE2",... "PartE13" to  "PartE.1", "PartE.2",... "PartE.13", (note the period between "PartE" and the digit) then you wouldn't even need to add all of the fields to the Sum calculation. You could just use the sum of field "PartE" and it would automatically summarize everyfield that started with  "PartE.X" even if you added a  "PartE.43" to the form at a later date.

              • 4. Re: Calculated sum requires fields to be mutliplied by 1
                Working for a living Level 1

                Thanks to all for your input.

                 

                While we have used the script provided for the column total, our calculations are sometimes all over the place and quite complicated.  The descriptions provided have at least confirmed when we will need to use the *1 and when we can use other options.

                 

                Thanks for the clarifications.