Skip to main content
andreah75641828
Known Participant
November 6, 2018
Answered

Round number to highest integer based on calculated text field

  • November 6, 2018
  • 1 reply
  • 1017 views

I'm not a java guy, but I've gone over the internet threads and couldn't find the exact fix to my problem. I'm using acrobat pro dc. I'm using a text field formatted to number with the following calculation:

(A+B+C+D)/4 in the simplified field notation.

I now need this calculation to always round up to the highest integer. Here are a few examples:

12.1=13

12.5=13

12.9=13

etc.

From what I've read, everyone says to use Math.ceil(); however, I cannot enter an actual number with Math.ceil() since I don't know the numbers because it's calculating them based on user input from other fields.

I believe it has something to do with inputting the following math equation:

( x + y - 1 ) / y

I just don't know how to implement it. Also, does it matter if I have number set to 0 or 2 for decimal places if I'm just looking for the higher integer?

Any custom calculation or validation scripts I can use to fix this? Thanks!

This topic has been closed for replies.
Correct answer Thom Parker

You'll need to use JavaScript for the calculation.

// Here's the calculation in Acrobat JavaScript

var A = this.getField("AField").value;

var B = this.getField("BField").value;

var C = this.getField("CField").value;

var D = this.getField("DField").value;

event.value = Math.ceil((A+B+C+C)/4);

The "AField", "BField", etc. are placeholders for the actual field names on your form.

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
November 6, 2018

You'll need to use JavaScript for the calculation.

// Here's the calculation in Acrobat JavaScript

var A = this.getField("AField").value;

var B = this.getField("BField").value;

var C = this.getField("CField").value;

var D = this.getField("DField").value;

event.value = Math.ceil((A+B+C+C)/4);

The "AField", "BField", etc. are placeholders for the actual field names on your form.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
andreah75641828
Known Participant
November 7, 2018

When I enter the script, it seems to not change anything. Below is my exact script:

var A = this.getField(PM_RAW_TOTAL).value;

var B = this.getField(PM_RAW_TOTAL_2).value;

var C = this.getField(PM_RAW_TOTAL_3).value;

var D = this.getField(PM_RAW_TOTAL_4).value;

event.value = Math.ceil((A+B+C+D)/4);

^^^(I'm assuming you meant D instead of C twice?)

Also, when I delete the total number out of the text field, it doesn't recalculate/repopulate.

Inspiring
November 7, 2018

This line:

var A = this.getField(PM_RAW_TOTAL).value;

Should be this:

var A = this.getField("PM_RAW_TOTAL").value;

Same for the others. Always check the JavaScript console (Ctrl+J) for errors that will help you troubleshoot.