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

Rounding up in Acrobat

Constance
Registered: Mar 17 2011
Posts: 13
Answered

Hi - I have a form that I need to code to round up. The first field called Submit.1 gives the actual total. and the second field named RSubmit.0 is suppose to be a rounded field of Submit.1. The form was originally created in Excel, and the Excel code is: =ROUNDUP(H32,-3)
 
Can anyone help with the correct script for this?
 
Thanks

My Product Information:
Acrobat Standard 8.0, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Was your form created in Acrobat or LiveCycle Designer?

You have a previous post about using LiveCycle Designer.

LiveCycle Designer has the built-in function "Ceil" that rounds the passed parameter to the next smallest value and allows for specification of the precision (decimal places).

If your form was created in Acrobat, you will need to add user written code using the 'Math.ceil' method of JavaScript. This method only works at the integer level and you will need to adjust the precision using the "Math.pow" method.

George Kaiser

Constance
Registered: Mar 17 2011
Posts: 13
The form was previously in Microsoft Excel, but has been converted to Adobe Acrobat. I have written in several forums as I have not received an answer that has worked. I'm not familiar at all with the "Math.cell" method of JavaScript and how to write it.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You will have to code JavaScript within the Acrobat form, since there are no 'helper' UI like some of the optional calculations methods and those helper UI's do not accept any JavaScript properties or methods.

The JavaScript Math.ceil method takes one one parameter, the number or round up to the next smallest integer.
.
So a custom calculation script for the 'RSubmit.0' field for rounding up to the next smallest integer of the field 'Submit.1' could be:
.
// get value of field to round up
var nValue = this.getField("Submit.1").value;
// set field to rounded up value
event.value = Math.ceil(nValue);
.
But this will not adjust for the precision, decimal point location for rounding up. So you need to adjust the precision of the value prior to rounding up and then adjust the decimal point back to the original location. You will need to use the Math.pow method for adjusting the precison or decimal place.
.
// get value of field to round up
var nValue = this.getField("Submit.1").value;
// value for adjusting the precision, decimal point.
var nPrecision = -3;
// adjust sign of nPrecision for exponentiation
nPrecsision = -nPrecision;
// compute the needed decimal adjustment
var nAdjustment = Math.pow(10, nPrecision);
// adjust the decimal point
nValue = nValue * nAdjustment;
// rounded up value
nValue = Math.ceil(nValue);
// restore the original decimal point location and display the result
event.value = nValue / nAdjustment;
.
The code can be shortened, but as written, it shows the steps needed for the calculation in short steps.
.
Since this type of calculation could be used several times within a form, I would create a document level function to perform this calculation. This would allow one to just call the defined function with the proper parameters and have the rounded up value returned to the calling script.
.
The document level function could be:
.
function Ceil(nValue, nPrecision) {
if(typeof nPrecision == "undefined")
nPrecision = 0;
// rounded up the adjusted value
return Math.ceil(nValue / Math.pow(10, -nPrecision) ) * Math.pow(10, -nPrecision);
} // end Ceil function
.
The custom calculation for the field them becomes:
.
// get value of field to round up
var nValue = this.getField("Submit.1").value;
// roundup
event.value = Ceil(nValue, -3);

George Kaiser

Constance
Registered: Mar 17 2011
Posts: 13
I've tried this code and it doesn't work.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Look at 3 Rounding Methods for a working form using user written Round, Ceil, and Floor functions.

George Kaiser

Constance
Registered: Mar 17 2011
Posts: 13
Thanks George, I was able to view the 3 Rounding Methods and compared it to mine and was able to get it to work.

Connie
Constance
Registered: Mar 17 2011
Posts: 13
Hi George - it's me again. After following your script and getting the field to work, the owner would like to have it round up - for example, if Subtotal.1 is $9,144.00, then they want RSubtotal.0 to round up to $10,000. Is this possible?

Connie Bretes
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
If you use '-3' for the precision, the 'Ceil' function will round 9,####.## to 10,000.00.

Are you editing your form in Acrobat or LiveCycle Designer?

Within the Acrobat product line, there are 2 programs for creating and editing forms, Acrobat and LiveCycle Designer. Each program can use JavaScript, but the language syntax is different and LiveCycle Designer also has the FormCalc language. Unfortunately the 2 products are not interchangeable, so whichever product you create you form in, you must always use that product for editing or start over. LiveCycle has a function called "Ceil" that will perform the same action as Excel's "ROUNDUP'.

George Kaiser

Constance
Registered: Mar 17 2011
Posts: 13
I'm editing in Acrobat. Where would you put the -3 to make this round up 9,###.## to 10,000.00?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
If the function definition is:

function Ceil(nValue, nPrecision) {

Then there are 2 parameters, 'nValue' for the value to process and the 'nPrecision' for the precision to use in the computation. The function will default the 'nPrecision' to zero if that parameter is omitted.

The custom calculation script in sample code posted:

// get value of field to round up
var nValue = this.getField("Submit.1").value;
// roundup
event.value = Ceil(nValue, -3);

Gets the value of the "Submit.1" field as variable 'nValue'. This value is then passed to the "Ceil" function as the value to process and the "-3" is the precision to use for the number of decimal place adjustment for the thousand (10^3).

George Kaiser

Constance
Registered: Mar 17 2011
Posts: 13
Thanks much George, this formula works great.
Constance
Registered: Mar 17 2011
Posts: 13
Does anyone know if there is a good book regarding Adobe Acrobat Javascript that would be worth buying? Something that covers stuff like what is covered above, etc.?

Thanks
Connie
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
There are not many because the scripting language is so specific to a given product and most computer software is updated so quickly, that it is hard to keep printed documentation updated.

extending Acrobat Forms with JavaScript by John Deubert of Acumen Training is a good starting point, but it is a bit dated.
.
Ted Padova has written a the PDF Bible for various versions of Acrobat and with Angie Okamoto has written PDF Forms Using Acrobat and LiveCycle Designer Bible.
.
You and also find more information about Acrobat JavaScirpt at JavaScript for Acrobat provides documentation for Acrobat JavaScript specific objects, properties and methods.
.
For the overall JavaScript documentation see Mozila Developer Center - JavaScirpt.
.
The Learning Center here has a number of tutorials and videos about using JavaScript in Acrobat.

George Kaiser