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

Help changing a negative result to $0.00

techwriter
Registered: Dec 19 2007
Posts: 20

I'm a complete novice with javascript and programming in general, so I need help with something I'm trying to accomplish in Acrobat. I've created an expense form with some simple calculations. What's giving me trouble is how certain results are being displayed. Two of the results deal with a cash advance and how much money is due to the employee, or how much is owed to the company by the employee (fields: cashdue and cashowed). I used a subtraction javascript to calculate the results:

cashdue
Var a=this.getField("cashsubtotal").value;
Var b=this.getField("cashadvance").value;
event.value=a-b

cashowed
Var a=this.getField("cashsubtotal").value;
Var b=this.getField("cashadvance").value;
event.value=b-a

The problem is, I'm getting the same result in each field (one positive, the other negative), depending on the data entered. What I want to do is write an if/then script that will make the result show as $0.00 if it is a negative number. If someone could give me a hand I would appreciate it. I found the subtraction script on this forum, and reading articles on if statements has only made me more confused.

Thanks

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You need to add one of the "if" statements to restrict the answer to only posative results:

// cashdue
var a=this.getField("cashsubtotal").value;
var b=this.getField("cashadvance").value;
event.value=a-b // compute due
if (event.value < 0) event.value = 0; // if negative set result to zero// cashowed
var a=this.getField("cashsubtotal").value;
var b=this.getField("cashadvance").value;
// set event value to b-a if "b - a > 0" : else set it to 0
event.value = b - a > 0? b-a : 0;Note: original used 'Var' instead of 'var'.

George Kaiser

techwriter
Registered: Dec 19 2007
Posts: 20
That worked great. Thanks.
todd0168
Registered: Oct 1 2009
Posts: 13
gkaiseril wrote:
You need to add one of the "if" statements to restrict the answer to only posative results:// cashdue
Var a=this.getField("cashsubtotal").value;
Var b=this.getField("cashadvance").value;
event.value=a-b // compute due
if (event.value < 0) event.value = 0; // if negative set result to zero
I'm trying to get a field to report either 0 (if the result is negative) or the calculation if not, so I entered just the script above for my fields. (I'm assuming as long as I change the names it's not important). Anyway, I copied that script in and changed the field names to match and I keep getting an error that I am missing a ; before statement 1 in line 2. Not sure why or what that means.

Here is my script:

Var a=this.getField("GarnDispPay").value;
Var b=this.getField("TotWthhld").value;
event.value=a-b
if(event.value < 0) event.value = 0;and the error is:

missing ; before statement 1: at line 2

and it highlights this:

Var b=this.getField("TotWthhld").value;
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The keyword for variable is 'var' and not 'Var'. Acrobat JavaScript thinks that 'Var' is an object or variable name that is not defined.

var a = this.getField("GarnDispPay").value;var b = this.getField("TotWthhld").value;event.value = a - bif(event.value < 0) event.value = 0;

George Kaiser

todd0168
Registered: Oct 1 2009
Posts: 13
perfect thanks!
trinitydub
Registered: Feb 10 2009
Posts: 1
woohooo!!!!! work perfect... thanks gkaiseril