4 Replies Latest reply on Apr 14, 2017 12:28 PM by ag0419

    Subtraction issues!

    ag0419

      Hello,

       

      I'm attempting to make a PDF that calculates fields for me by using the data from other fields.  I have the calculation all set up to subtract, however I would like the field to show "0" if the number is negative instead of the negative number.  I'm not sure of the script to use for that.  Here is the script I have so far:

       

      var total = this.getField("BenefitMonthlyTotal").value - this.getField("EmployerContribution").value;

      this.getField("TotalMonthlyCost").value=total

       

      What could I add to this script so that all negative numbers show as a "0" and all positive numbers show as actual number?

       

      Thank you for your help!

        • 1. Re: Subtraction issues!
          gkaiseril MVP & Adobe Community Professional

          There are conditional control statements. One of these statements is the "if" statement. This statement controls the execution control of the script based on a logical statement being true. The logical statement could be a comparison of numeric values.

           

          So if I have a field named "TotalMonthlyCost" and that field has a custom JavaScript calculation calculates the Monthly Benefit Total and if that total is less than zero, then the Monthly Benefit Total is zero, else it is the computed value;

           

          var total = this.getField("BenefitMonthlyTotal").value - this.getField("EmployerContribution").value;

          if(Total < 0) {

          event.value = 0;

          } else {

          event.value = total;

          }

           

          Note that if the field being processed is the field with the current focus, one uses the event object and the field object.

          • 2. Re: Subtraction issues!
            ag0419 Level 1

            Unfortunately that did not work.  If I type it exactly as you have above it stays at the same number as the Employer Contribution.  It doesn't subtract.  Here is a little more detail:

             

            I have a 3 fields Benefit Monthly Total (which is a calculated field adding up numbers in other fields), Employer Contribution (which is a field that has the same number constantly, does not change), and Total Monthly Cost (which should be the Benefit Monthly Total minus the Employer Contribution).  I am able to subtract the two fields and get the correct answer.  I would like the Total Monthly Cost field to either populate the answer if the answer is a positive number or leave the field as "0" if the number is negative.

             

            Thank you for your help!!!

            • 3. Re: Subtraction issues!
              Joel_Geraci Adobe Community Professional

              if(Total < 0) {

               

              Should be changed to a lower case "t" in Total.

               

              J-

              1 person found this helpful
              • 4. Re: Subtraction issues!
                ag0419 Level 1

                Thank you both, that worked perfectly!!!