6 Replies Latest reply on Jan 25, 2017 2:02 PM by gkaiseril

    Custom calculating script question

    kevinp57542651

      Hello,

       

      I am running this custom script to figure a field, problem is they want the sum to be a percentage rounded to 4 decimal points, but when I set the field to be a number and limit it to the 4 decimal points, the field gives an error because the custom script is putting a "NaN" in the field when there are no numbers to calculate. This NaN is not a number and causes the error message that it does not comply with the allowed format of the field.

       

      // Get first field value
      var v1 = getField("DSW-Total Payments").value;

      // Get second field value
      var v2 = getField("DSW-Total Monthly Income").value;

      // Set this field value equal to the difference
      event.value = v1 / v2 * 100;

       

      Any help would be greatly appreciated.

        • 1. Re: Custom calculating script question
          Karl Heinz Kremer Adobe Community Professional

          You need to change your script so that it only performs the calculation if it has the two input values - and otherwise clears the field (sets it to an empty string):

           

          // Get first field value
          var v1 = getField("DSW-Total Payments").valueAsString;
          // Get second field value
          var v2 = getField("DSW-Total Monthly Income"). valueAsString;
          
          
          if (v1.length>0 && v2.length>0) {
              // Set this field value equal to the difference
              event.value = Number(v1) / Number(v2) * 100;
          }
          else {
              event.value = "";
          }
          
          • 2. Re: Custom calculating script question
            try67 MVP & Adobe Community Professional

            Checking the length of the values is not enough. The value of v2 could be "0" and then the length is 1, which would still cause an issue.

            I would recommend doing it like this:

             

            // Get first field value 
            var v1 = Number(this.getField("DSW-Total Payments").valueAsString);
            // Get second field value 
            var v2 = Number(this.getField("DSW-Total Monthly Income").valueAsString); 
            
            if (v2!=0) { 
                // Set this field value equal to the difference 
                event.value = Number(v1) / Number(v2) * 100; 
            } else { 
                event.value = ""; 
            } 
            
            • 3. Re: Custom calculating script question
              kevinp57542651 Level 1

              Hello,

              I gave it a try and I am still getting the error message when I clear the form because it wants to default back to the NaN when cleared. Is there a way to limit the decimals or round it to 4 decimals within the javascript so that I don't have to use the format tab? The error reads, "The value entered does not match the format of the field [DSW-Proposed Debt Ratio]

               

              When I turn off the Number format to the field, I do not get the error message, but then is puts an infinite number of decimal numbers. They want it restricted to round to 4 decimal places.

              • 4. Re: Custom calculating script question
                Karl Heinz Kremer Adobe Community Professional

                Yes, you can do that. Use the Number.toFixed() method:

                 

                  event.value = Number(Number(v1) / Number(v2) * 100).toFixed(4);
                

                 

                You would still have to make sure that you catch any NaN condition (which you can test for using isNaN()).

                1 person found this helpful
                • 5. Re: Custom calculating script question
                  kevinp57542651 Level 1

                  Than you so much! I had been scouring the internet for days. Your help is greatly appreciated.

                  • 6. Re: Custom calculating script question
                    gkaiseril MVP & Adobe Community Professional

                    I would use 2 scripts for the result field. One to format the result as a percentage when there is a non-null or non-zero result and the other a custom calculation to compute ratio.

                     

                    // custom format script for displaying the ratio when data present and a null sting when there is no valid result;

                    // format for this field is "None", formatting will be applied within this script;
                    if(event.value != "" || event.value == 0)
                    {
                         AFPercent_Format(4, 0, false);
                    }
                    // end custom format script;

                    // custom calculation script for the ratio;
                    // Get first field value
                    var v1 = getField("DSW-Total Payments").valueAsString;
                    // Get second field value
                    var v2 = getField("DSW-Total Monthly Income").valueAsString;
                    // clear the field;
                    event.value = "";
                    // compute the result if we have 2 values and the divisor is not zero;
                    if (v1.length > 0 && v2.length > 0 && Number(v2) != 0) {
                        // Set this field value equal to the difference
                        event.value = Number(v1) / Number(v2);
                    }
                    // end custom calculation script;