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

Rounding issue

jmarmst
Registered: Mar 9 2010
Posts: 5
Answered

I created a pdf form that mulitiplies s.t. hours (5.5) in one column x rate per hour (29.15) in the second column. I have the result in a third column formatted for 2 decimal places. The result shows as 160.33 on the form; calculates to 160.325. Then I multiply the result (160.33) x an overhead percentage in another spot on the form (.55%) with a result of (88.18). My subtotal value of the 160.33 plus the overhead amount of 88.18 shows as 248.50. Is there a script that will round the subtotal so it shows 248.51? I'm new at this so any help you can give me would be appreciated.

Looks like this:

Hours Rate Dollar Amount Labor OH 55% Sub-Total
5.5 29.15 160.33 88.18 248.50

My Product Information:
Acrobat Pro 9.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The issue here is the real value of the field. Setting the format option for a field only changes how the data is displayed to the user. The real underlying value is unchanged. You can see this by removing the formatting on the fields, and you'll also see that the sub-total calulation is correct for the values that are provided.

If you want the actual values of the fields to be rounded you'll have to do some more work, this is not a feature that is built into Acrobat. One of the simplest ways to do this is to add the following code to the Validation event of the calculated fields:

event.value = util.printf("%02.2f",event.value);

This code is for an AcroForm, which from your post is what I believe you are using.

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

jmarmst
Registered: Mar 9 2010
Posts: 5
I copied your code to the Text Field Properties, under the Validate tab, in the Run custom validation script, but I get the same result on my form. I want to release this form to our dispatcher, but I know she'll say $160.33 and $88.18 don't equal $248.50 when she goes to use this form. It's probably something simple, but I'm not seeing it.

Thank you for your time on this!!
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
I believe you will need to round both the 'Dollar Amount' and the 'Over Head' calculations.

For the 'Dollar Amount' field you could use the following custom JavaSript calculation:
// get the field values for hours and pay ratevar hours = this.getField('hours').value;var rate = this.getField('rate').value// compute the pay amountvar pay = this.getField('hours').value * this.getField('rate').value;// round the computed pay amountpay = util.printf('%,1.2f', pay);// set the pay field valueevent.value = pay;

Then for the 'Over Head' calculation you could use the following custom JavaScript calculation:
// get the round pay valuevar pay = this.getField('dollarAmount').value// compute the over headvar oh = pay * 0.55;// round the over head amountoh = util.printf('%,1.2f', oh);// fill in the over head valueevent.value = oh

Since you will be using the rounding process many times, you might want to create a document level function to perform the rounding function.
function Round(fValue, iDec){// function to round a number // to the passed number of decimal placesfValue = Number(fValue); // convert string to numberiDec = Math.floor(iDec); // whole decimals only// define format stringvar sFormat = '%,1.'; // numeric format dot decimalsFormat += iDec; // add number of decimalssFormat += 'f'; // for floating point values// return rounded numberreturn util.printf(sFormat, fValue);}

You can then replace the 'util.printf('%1.2f', value)' with "Round(value, 2)' in the scripts.

George Kaiser

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I recreated the calculations you described (very well I might add) in the first post. After adding the validation script to the Dollar amount, the Labor OH, and the subtotal I got the results you were looking for. Rounding the real value of the field is most important on the Dollar Amound and the Labor OH since these are the fields that feed into the final sum.

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

jmarmst
Registered: Mar 9 2010
Posts: 5
Thom,

Works like a charm!! Thank you for spending your valuable time on this.

Jean