Calculated field that rounds down, but if the result is 0, show the result as 1 instead

Community Beginner ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Hello! I have a form that has multiple calculations, and one thing I've been asked to do is to divide one field by another and show the result in a third field. This third field should round down to the nearest whole number. However if the nearest whole number is 0, the field should show 1 as the result. I've looked online for javascript to put in the Custom Calculation Script window, but nothing has worked so far. Can someone help me figure this out?

TOPICS
JavaScript , PDF forms

Views

51

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jun 22, 2022 Jun 22, 2022

Lets say you have "Field A", "Field B" and "Field C", you can use this as custom calculation script of "Field C":

var a = Number(this.getField("Field A").value);
var b = Number(this.getField("Field B").value);
if(b != 0)
var x = Math.floor(a/b);
if(x==0)x=1;
event.value = (a&&b)?x:"";

Likes

Translate

Translate
Adobe Employee ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Hi there

 

Hope you are doing well and sorry for the trouble.

 

The workflow that you are trying to achieve is possible using JavaScript. For more information please check the help pages listed below:

https://helpx.adobe.com/acrobat/using/applying-actions-scripts-pdfs.html

https://acrobatusers.com/tutorials/javascript_console/

 

Regards

Amal

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
community guidelines
Community Beginner ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Thank you for posting the links, Amal!

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
community guidelines
Adobe Community Professional ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Lets say you have "Field A", "Field B" and "Field C", you can use this as custom calculation script of "Field C":

var a = Number(this.getField("Field A").value);
var b = Number(this.getField("Field B").value);
if(b != 0)
var x = Math.floor(a/b);
if(x==0)x=1;
event.value = (a&&b)?x:"";

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
community guidelines
Community Beginner ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

LATEST

Nesa - Thank you so much!! This works perfectly and you laid it out in an easy to read and modify format! 🙂

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
community guidelines