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

Whole number in calculation ??

Jamesone
Registered: Jul 31 2011
Posts: 15
Answered

I have a PDF that starts at (which has been worked out manually)
 
$0.01 to $122.14 =1 UNIT
$122.15 to $244.28 =2 UNITS
$244.29 to $366.42 =3 UNITS
 
and so on this is based on a Unit whose value is $122.14.
 
I want to create a calculation txt field on a FORM where the user inputs a dollar amount and the result is displayed for the correct amount of units.
 
so if I divide the unit $122.14 into say the first row .05 cents I get a result of .0004093.
 
It has to be a whole number 1 UNIT if the user inputs .05.
 
So Txt1 (dollar amount) divide by Txt2 ($122.14) gives me a whole number in Txt3 and not .0004093.
 
Any idea how to do this???????
 
Thanks James

maxwyss
Registered: Jul 25 2006
Posts: 255
If you do no longer need the exact value, you could add the following script to the Validate event of the according field:

event.value = Math.ceil(event.value*1 / 122.14) ;

and that should give you the correct number of units.

Hope this can help.

Max Wyss.

Jamesone
Registered: Jul 31 2011
Posts: 15
Max wyss,
Thanks for your reply, that really helps and works, now I have a problem with the script.

Say the dollar amount is input into Text1 I would like the result to be in Text2 and

If the Units exceed 100, I need it to say that Max Units exceeded in the Text2 field......

Is this possible???
maxwyss
Registered: Jul 25 2006
Posts: 255
Sure, this is possible.

a) if you simply want to limit the number of units, without notification, you have a simple one-liner in the Validate event:

event.value = Math.min(100, Math.ceil(event.value*1 / 122.14)) ;

and that should take care of it.

b) if you want a notification, you could evaluate the number of units you would get beforehand, and that would look like this:

if( Math.ceil(event.value*1 / 122.14) > 100) {
app.alert("You have exceeded the maximum number of units") ;
}
event.value = Math.min(100, Math.ceil(event.value*1 / 122.14)) ;

and that should do it.

Hope this can help.

Max Wyss.

Jamesone
Registered: Jul 31 2011
Posts: 15
Howdy Max,

Thanks for the help so far with this, I have tried both a and b solution, a is fine but it shows 100 in the text field after going over the dollar amount regardless.

B is fine with the pop up warning but when you close it 100 appears in the text field, which would most likey cause the user to give out that number and cause all sorts of headaches.....

Is there anyway to leave the Text field blank if a number greater than 100 appears in it. ????

Thanks
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You could use the validation event to check the number of units and set the event's return code:

Custom calculation script:

// name of input field
var cInput = "Text1";
// get value of input field
var nAmount = this.getField(cInput).value;
// compute units
event.value = Math.ceil(Number(nAmount) / 122.14);

And then use the validation script:

if(event.value > 100) {
app.alert("Maximum numbrer of units, 100 units, has been exceeded", 1, 0, "Numnber of Units Exceeded!");
event.value = 0; // clear the field
event.rc = false; // set return code
}

Or modify your custom calculation script:

// name of input field
var cInput = "Text1";
// get the imput field value
var nAmount = this.getField(cInput).value;
// compute the value to post
var nUnits = Math.ceil(Number(nAmount) / 122.14);
// test number of units
if( nUnits > 100 ) {
// over 100 units report error
app.alert("You have exceeded the maximum number of units", 1, 0, 'Maximun Units Error");
nUnits = ""; // clear value to post
}
// post the value
event.value = nUnits;

George Kaiser

Jamesone
Registered: Jul 31 2011
Posts: 15
Hi George K,

Thanks for the scripts.

Tried the Custom and Validation script which works, but the validation script does not set Text2 to O if greater than 100 units.

If you type in Text1 12200 which puts 100 in Text2, then type in Text1 12500 which brings up exceed units box, it does not reset Text2 to ZERO.

Could'nt get the " Or modify your custom calculation script: " to work, came up with

Syntax Error unterminated string literal 10:at line 11.

If you could somehow weave your magic and correct this it would be a great help.

Thanks

PS Would a quick & dirty way of doing this would be to put on mouse exit of Text1 (RESET FORM) which clears Text2 field
try67
Expert
Registered: Oct 30 2008
Posts: 2398
The script is not working because there's an error in it. From the error message it seems that you forget (at least one) quotation mark around line 11. If you post your code, we could let you know what exactly the problem is.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Jamesone
Registered: Jul 31 2011
Posts: 15
Hi the code is below,

// name of input field
var cInput = "Text1";
// get the imput field value
var nAmount = this.getField(cInput).value;
// compute the value to post
var nUnits = Math.ceil(Number(nAmount) / 122.14);
// test number of units
if( nUnits > 100 ) {
// over 100 units report error
app.alert("You have exceeded the maximum number of units", 1, 0, 'Maximun Units Error");
nUnits = ""; // clear value to post
}
// post the value
event.value = nUnits;

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
In the app.alert command you have a string that starts with a single quote and ends with a double quote...

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Jamesone
Registered: Jul 31 2011
Posts: 15
Thanks try67, I could'nt see the forest for the trees and George for the original scripts.

That finishes that little project...

Correct code for those that need it..............

// name of input field
var cInput = "Text1";
// get the imput field value
var nAmount = this.getField(cInput).value;
// compute the value to post
var nUnits = Math.ceil(Number(nAmount) / 122.14);
// test number of units
if( nUnits > 100 ) {
// over 100 units report error
app.alert("You have exceeded the maximum number of units", 1, 0, "Maximun Units Error");
nUnits = ""; // clear value to post
}
// post the value
event.value = nUnits;