1 Reply Latest reply on Apr 26, 2016 2:03 PM by George_Johnson

    How to use if/then for a calculation field

    jdd561

      I'm trying to create a fillable form that has a simple calculation for work.  The calculation involves 2 fields, and I can get it to run in Excel, but Acrobat just weirds out any time there are 0s involved - I know its a calculation problem.

       

      So here is the scenario.  I have a field called 'Acres1', and a field called "WAF1".  My results field is called "WAI1".  There are potentially 10 lines of this code (Acres2, Acres3,...WAF2, WAF3...etc).

       

      My formula works under simple calculation as ((WAF1/12)/Acres1) so long as Acres1 and WAF1 are populated with numbers.  However, everything bombs out with error boxes if both or either of those fields are not populated - and I won't always have the whole 10 lines of data populated.  I've been banging my head against the wall all afternoon trying to figure this out, and to no avail. 

       

      Help!!!

        • 1. Re: How to use if/then for a calculation field
          George_Johnson MVP & Adobe Community Professional

          When the Acres field is blank or zero, you're dividing by zero, which results in either + or - Infinity, or the special numeric value of NaN. If the field is formatted as a number, any of these values will cause an error in the formatting routine. What you probably want is if the Acres field is blank, then the result of the calculation is blank as well. You need to use JavaScript for this:

           

          // Custom calculation script

          (function () {

           

              // Get the field values, as numbers

              var waf = +getField("WAF1").value;

              var acres = +getField("ACRES1").value;

           

              // Set this field's value

              if (acres !== ) {

                  event.value = waf / 12 / acres;

              } else {

                  event.value = "";

              }

           

          })();

           

           

          Since you have multiple rows, you should make this into a document-level function so it can be called from the calculation script of each WAI field. It would make things easier if you changed the field names so there is a separator character between the field name and number, like: WAF_1 and ACRES_1

           

          This makes it easier to get the row number from the field name. Post again if you need more help.