Skip to main content
Participant
Jan 25, 2017 8:20 PM UTC
Answered

Custom calculating script question

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 is closed to new replies. Start a new post to keep the conversation going.
Karl Heinz  Kremer
Karl Heinz KremerCorrect answer
Community Expert
Jan 25, 2017 9:50 PM UTC

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

6 replies

Inspiring
Jan 25, 2017 10:02 PM UTC

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
Karl Heinz  Kremer
Community Expert
Community Expert
Jan 25, 2017 8:33 PM UTC

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
try67
Community Expert
Community Expert
Jan 25, 2017 9:13 PM UTC

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