Custom Calculation Script: Avoid Division by 0. Prevent "Value Entered Does Not Match Format of the Field"

I have been throughout this site and have discovered that I am having a problem b/c Acrobat XI thinks that I could be dividing by zero, however, I do not know what I need to put in my scripts to avoid division by 0.

My scripts work fine as long as I do not change the format from "none" to "number". Bottom line is I need the values to round to one or two decimal places without producing the error.

My scripts are as follows:

event.value = this.getField("DistanceInches1").value / ( this.getField("TimeSeconds1").value / 60 + this.getField("TimeMinutes1").value );

event.value = ( this.getField("LowAmps1").value * this.getField("LowVolts1").value * 60 ) / ( this.getField("IPM1").value * 1000);

In order to clarify, what do I need to have in the script so that I can format the field as a number with decimal(s) and prevent the error.

Thanks in advance.

Quality Testing

2 Answers

Voted Best Answer

Another question is, do you need the calculated value to be rounded. Note that the rounded display value may not be the same as the actual field value or the computed value. This difference could cause additional issues if the computed value is used in another calculation.

Since your field appear to have a row or column index, will these calculations be performed more than once with different field values.

Your first equation:

// row / column index
var nIndex = 1;
// field names
var DIName = "DistanceInches" + nIndex;
var TSName = "TimeSeconds" + nIndex;
var TMName = "TimeMinutes" + nIndex;

// get field values as numbers
var DistanceInches = Number(this.getField(DIName).value);
var TimeSeconds = Number(this.getField(TSName).value);
var TimeMinutes = Number(this.getField(TMName).value);
event.value =  ""; // default value for no calculation; 
if(TimeSeconds != 0 || TimeMinutes != 0) {
event.value = DistanceInches / (TimeSeconds / 60 + TimeMinutes); 
// some debugging information for example;
console.println("Distance inches: " + DistanceInches);
console.println("Time seconds: " + TimeSeconds);
console.println("Time minutes: " + TimeMinutes);
console.println("Result unrounded: " + event.value);
console.println("Result rounded: " + util.printf("%,0 0.2f", event.value));

Try different values and observe the results in the JavaScript console to see that the displayed value is not the exact value of the computation or field.

By George Kaiser   

Acrobat does not "know" about any of the numbers you are using in your calculation. JavaScript is like any other programming language: You have to either make sure that you have valid data, or you need to catch the exception that JavaScript throws when the error occurs.

I usually do something like this:

var a = this.getField("DistanceInches1").value;
var b = this.getField("TimeSeconds1").value;
var c = this.getField("TimeMinutes1").value;

if (b != "" && b != 0)
    event.value = a/b + c;
    event.value = "";

If you would be dividing by 0, you would end up with an empty result field.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...

Karl Heinz Kremer   

Please specify a reason: