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

Form calculation weirdness

rbr00x
Registered: Oct 14 2009
Posts: 26
Answered

I am doing what I thought was a straightfoward calculation, but something weird is happening. This is a form that adds up insurance premiums and subtracts one field. I am using a script to add an extra $35 if the swimimng pool checkbox is set to yes. It works fine if I enter premiums then check the box. However, if I check the box first then enter premiums, the math goes haywire.

For example, if I check the box, the total goes to $35, which is correct, but if I then enter $100 in the first premium field, the total goes to $10,035 instead of $135.

I am using Acrobat Pro 9. I have placed this code in the custom calculation field...

//add pool surcharge to total
if

(this.getField("swimmingPool").value == "Yes") {

event.value = this.getField("premium_calc").value + this.getField("autoCredit").value + 35;

}else {event.value = this.getField("premium_calc").value + this.getField("autoCredit").value;
}

My Product Information:
Acrobat Pro 9.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Looks like the calculation is adding text instead of numbers. Try this, move the main caculion out of the if statment and make sure it's performing numerical calculations.
event.value = Number(this.getField("premium_calc").value) + Number(this.getField("autoCredit").value);if (this.getField("swimmingPool").value == "Yes")event.value += 35;

This forces the values into the correct context. However for it to work the initial values for the fields have to be 0, not empty strings, which is what probably caused the calc to be in a string context in the first place.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
JavaScript tends to be rather lose in the handling of text strings of numbers and numbers. It appears JavaScript is treating the values as character strings and not numbers. You can use the 'Number()' constrictor to force the calculation to treat the values as numbers and not character strings. The '+' operator is not only the arithmetic add operator but also the string concatenate operator.
//add pool surcharge to totalif (this.getField("swimmingPool").value == "Yes") {event.value = 35 + Number(this.getField("premium_calc").value) + this.getField("autoCredit").value;}else {event.value = this.getField("premium_calc").value + this.getField("autoCredit").value;}

George Kaiser

rbr00x
Registered: Oct 14 2009
Posts: 26
Thank you both for the quick response. I would not have figured that out on my own!