Round number to highest integer based on calculated text field

Community Beginner ,
Nov 06, 2018

Copy link to clipboard

Copied

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!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

195

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Round number to highest integer based on calculated text field

Community Beginner ,
Nov 06, 2018

Copy link to clipboard

Copied

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!

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

196

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 06, 2018 0
Adobe Community Professional ,
Nov 06, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 06, 2018 3
Community Beginner ,
Nov 06, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 06, 2018 0
Adobe Community Professional ,
Nov 06, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 06, 2018 2
Community Beginner ,
Nov 07, 2018

Copy link to clipboard

Copied

Oh shoot, I didn't even realize they had a console on here lol my bad. Thanks, the quotes fixed it!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 07, 2018 0
Community Beginner ,
Nov 07, 2018

Copy link to clipboard

Copied

Awesome! This worked perfect, thanks!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 07, 2018 0