These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Field Calculation help on changing neg numbers to 0

kruegerand
Registered: Oct 22 2008
Posts: 4
Answered

I'm very new to Java in general and am currently working on a form which will calculate simple math for various fields. Some fields will end up with a negative value, in which case I would like to have a script at the field level, replace the negative number with "0". I know this is probably a very simple thing, but would someone mind providing help, or an example for me.

Thanks,

My Product Information:
Acrobat Pro 8.0, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can use the "if" statement. At the end or your custom calculation script you will need to add:

// force event value to zero for negative result
if(event.value < 0) event.value = 0;

George Kaiser

maxwyss
Online
Registered: Jul 25 2006
Posts: 256
Another way to this would be using the Math.max() function.

Assumed your result is in the variable myResult, the last line in your calculation would be

event.value = Math.max(0, myResult) ;

and that would do it.

Hope this can help.

Max.
kruegerand
Registered: Oct 22 2008
Posts: 4
Thanks for both of your suggestions, I'm still very new to the syntax/format of java and am trying to evaluate the following simple expression from two fields within my form and cannot get it to work, even when substituting different options presented, what I want to do seems fairly simplistic, but I'm sure my structure is wrong:

var field1
var field2
var x

for (x=field1 - field2);
{if (x<0) x=0;}
else{x;}

Please bear with my ignorance and newness to this. Thank you for any and all assistance.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Assuming this is a custom calculation script for a field, the correct code would be something like:

// Get field values, as numbersvar v1 = +getField("Text1").value;var v2 = +getField("Text2").value;var result = v1 - v2; // Set this field's valueif (result < 0) {event.value = 0;} else {event.value = result;}

Replace Text1 and Text2 in the code above with the names of your fields. Be sure to set any calculated fields to readonly so that a user can't attempt to interact with them.

George
kruegerand
Registered: Oct 22 2008
Posts: 4
George,

Thank you, that worked beautifully.

I appreciate everyone's assistance.