Skip to main content
Participant
January 25, 2017
Answered

Custom calculating script question

  • January 25, 2017
  • 2 replies
  • 1150 views

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.

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

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

2 replies

Inspiring
January 25, 2017

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;

Karl Heinz  Kremer
Community Expert
Community Expert
January 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 = "";

}

try67
Community Expert
Community Expert
January 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 = "";

}