6 Replies Latest reply on Jun 6, 2012 8:08 AM by gkaiseril

    Calulate for division in Acrobat X

    Brenda VT Labor

      How do I calulate for division in Acrobat X? I do not know javascript and it appears that for simple division this might be what I have to figure out--fast. Thank you.

        • 1. Re: Calulate for division in Acrobat X
          George_Johnson MVP & Adobe Community Professional

          You have to make sure the if the denominator is zero, that you set the value of the field to something that makes sense, which is often to simply blank the field. For example, the following script divides one field value by another. But if the denominator field is blank or otherwise evaluates to zero when used as a number, the calculated field will be blanked.

           

          // Custom calculate script

           

          // Get the field values, as numbers

          var numerator = +getField("text1").value;

          var denominator = +getField("text2").value;

           

          // Calculate and set this field value

          if (denominator !== 0) {

              event.value = numerator / denominator;

          } else {

              event.value = "";

          }

           

           

          That last if/else block can be reduced to just the following

           

           

          // Calculate and set this field value

          event.value = denominator !== 0 ? numerator / denominator : "";

          • 2. Re: Calulate for division in Acrobat X
            gkaiseril MVP & Adobe Community Professional

            The "/" symbol is the division operator.

             

            JavaScript does return non-numeric values when division is by a null, zero, a very small denominator, or a very large divisor. So you may need to create conditional execution code using the "if" statement if you expect any of these conditions to apply.

            • 3. Re: Calulate for division in Acrobat X
              Brenda VT Labor Level 1

              I know nothing of java scrip and almost need English subtitles with this. I will try to figure out what you are saying.

               

              I need to calulate:

               

              Total Benefit Charges /Total Taxable Payroll = Benefit Ratio

              • 4. Re: Calulate for division in Acrobat X
                gkaiseril MVP & Adobe Community Professional

                Are you providing Field names or t the description of the fields?

                 

                // custom calculation script for the Benefit Ratio field

                // change for the field names

                var divisor = "Total Taxable Payroll";

                var cDividend = Total Benefit Charges";

                // DO NOT MAKE ANY CHANGES  BELOW THIS LINE

                // clear field value in cas no update

                event.value = "";

                // get the value of fields

                var nDivisor = this.getField(cDivisor).valueAsString;

                var nDividend = this.getField(cDividend).valueAsString;

                // process if we have a non-blank numeric divisor

                if(nDivisor != "") && Number(nDivisor) != 0 &&

                !isNaN(Number(nDivisor))){

                event.value = nDividend / nDivisor;

                }

                 

                You may need to learn some programing skills.

                • 5. Re: Calulate for division in Acrobat X
                  Brenda VT Labor Level 1

                  Those are field name. I have done programming in a past life. Was hoping ot avoid it in this one.  It looks like I have to learn java script whether I wnat to or not. YUk. I am the other side of the brain that enjoys the creative stuff more.

                   

                  Thank you.

                  • 6. Re: Calulate for division in Acrobat X
                    gkaiseril MVP & Adobe Community Professional

                    I just notice  errors in the code:

                     

                    The line:

                    var divisor = "Total Taxable Payroll";

                     

                    Should be:

                    var cDivisor = "Total Taxable Payroll";

                     

                    The line:

                    var cDividend = Total Benefit Charges";

                     

                    Should be:

                    var cDividend = "Total Benefit Charges";

                     

                    The line:

                    if(nDivisor != "") && Number(nDivisor) != 0 && !isNaN(Number(nDivisor))){ 

                     

                    Should be:

                    if(nDivisor != "" && Number(nDivisor) != 0 && !isNaN(Number(nDivisor))){ 

                     

                    // custom calculation script for the Benefit Ratio field

                    // change for the field names

                    var cDivisor = "Total Taxable Payroll";

                    var cDividend = "Total Benefit Charges";

                    // DO NOT MAKE ANY CHANGES  BELOW THIS LINE

                    // clear field value in cas no update

                    event.value = "";

                    // get the value of fields

                    var nDivisor = this.getField(cDivisor).valueAsString;

                    var nDividend = this.getField(cDividend).valueAsString;

                    // process if we have a non-blank numeric divisor

                    if(nDivisor != "") && Number(nDivisor) != 0 && !isNaN(Number(nDivisor))){  

                    event.value = nDividend / nDivisor;

                    }