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

JS beginner

Adam2013
Registered: Feb 25 2011
Posts: 2

Hello all, new here as you can tell. Also don't have much experience with JS. Only been in Java/C++/MATLAB classes so far.
 
I've got two text forms on Acrobat Pro, STRENGTHMOD and STR. I was wondering if there was a way to take the user-entered value from STR and:
 
divide by 2
subtract 5
then round up
 
and place that result in STRENGTHMOD using a calculation in JS?
 
In STRENGTHMOD, what I have so far (not working btw) is:
 
var ability = this.getField("STR");
var mod = (ability.value/2) - 5;
event.value=Math.ceil(mod.value);
 
Any and all help is very appreciated,
Adam

My Product Information:
Acrobat Pro 9.4.2, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
If you just want the displayed result rounded, then set the result field's format to "Number" and set the number of decimal palces to 2. If you want to force the result of the computation to 2 decimal places, you can use the 'util.printf' method or the "Math.round' method. If you use the "Math.round' you need to adjust the decimal place since 'Math.round' only rounds to integers.

// get field object
var ability = this.getField("STR");
// divide field's value by 2 and subtract 5
var mod = (ability.value / 2) - 5;
// format for rounding result
var cFormat = "%,1 .2f"
// format result
event.value = util.printf(cFormat, mod);

// some debugging:
console.show();
console.clear();
console.println('ability value: ' + ability.value);
console.println('mod (ability.value / 2) - 5: ' + ((ability.value / 2) - 5));
console.println('rounded with util.printf: ' + util.printf(cFormat, mod));
var nDecAdj = Math.pow(10, 2);
console.println('mod dec adjustment: ' + (mod * nDecAdj));
console.println('Math.round(' + (mod * nDecAdj) + '): ' + (Math.round(mod * nDecAdj)));
console.println('Shift decimal point back: ' + (Math.round(mod * nDecAdj) / nDecAdj));

George Kaiser

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Just change that last line to:

event.value = Math.ceil(mod);
Adam2013
Registered: Feb 25 2011
Posts: 2
Wow. Thank you both! I didn't expect two good answers so quickly! Check this one off as solved.