Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Custom calculating script question

New Here ,
Jan 25, 2017 Jan 25, 2017

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.

TOPICS
Acrobat SDK and JavaScript , Windows
991
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 25, 2017 Jan 25, 2017

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()).

Translate
Community Expert ,
Jan 25, 2017 Jan 25, 2017

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 = "";

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2017 Jan 25, 2017

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 = "";

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2017 Jan 25, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2017 Jan 25, 2017

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()).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 25, 2017 Jan 25, 2017

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 25, 2017 Jan 25, 2017
LATEST

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines