# Calculations in PDF form

New Here ,
Mar 13, 2021 Mar 13, 2021

Copy link to clipboard

Copied

I have 2 fields in my adobe form that I need to use for calculations:

____feet x ____inches

If the inches are greater than 0, I want to create a value = to 1 that can be added to the value of "feet" (in a hidden field) to be used in a subsequent calculation.

Basically rounding up the feet measurement.  I'm stumped. I don't know to enter the Javascript syntax.

TOPICS
JavaScript, PDF forms

Views

124

Likes

Report

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

## 1 Correct Answer

Adobe Community Professional , Mar 13, 2021 Mar 13, 2021
Your issue isn't with JavaScript, it's with how math is done. You know that feet and inches are different things, so adding them together makes no sense.  Your descripting is better. If there are any inches, then round up the total size to the next foot. So all you really need to know is if (inches > 0) You also didn't say where this code was placed, which is critical to the solution. I'll assume that it is a Calculation script in your hidden field. Try this: var feet = this.getField("feet").v...

Likes

5 Replies 5
New Here ,
Mar 13, 2021 Mar 13, 2021

Copy link to clipboard

Copied

What's wrong with this Javascript?

// get field values;

var feet = this.getField("feet").value;

var inches = this.getField("inches").value;

{event.value = feet+inches; event.value

= Math.ceil(event.value); // Round up to next largest integer; }

Likes

Report

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

Copy link to clipboard

Copied

Well, I am still learning JavaSript, but I can say that the syntax is wrong and the intent of your current calculation remains unclear to me.

My main observation is that you're not using the correct conversion in your formula.

You can't add feet and inches together when you're dealing with metric Length conversions.

If you can provide more details about what you're trying to achieve it'll make it easier to assist you.

For example, your example in your first reply above multiplies feet x inches.

I assume this is to convert feet to inches first?

And if that value is greater than 0 round it as whole number ?

or rounding it to nearest hundred?

or to the nearest whole number?

or just make it 1?

What exactly is the calculation that you need to use it with?

In any case, if you're converting feet to inches the total value is rounded up to the nearest whole number.

So, event.value = feet+inches is wrong unless that conversion of feet to inches occured first.

It should be something like:

Math.ceil((feet*12)+inches)

This will only result in a value for total inches. To convert this total to feet you need to divide it by 12.

The event value for this equation should be expressed using one more variable:

``````var inches = this.getField("inches").value;

var feet = this.getField("feet").value;

var inchesTotal = Math.ceil((feet*12)+inches);

if (inchesTotal > 0) event.value = 1;

``````

So basically what I assume yuo're trying to achieve is take these small inch length measurements and  if it is anything greater than 0 convert it to one foot length?

What happens if the value is less than 0 , like a negative number?

Likes

Report

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

Copy link to clipboard

Copied

Your issue isn't with JavaScript, it's with how math is done. You know that feet and inches are different things, so adding them together makes no sense.  Your descripting is better. If there are any inches, then round up the total size to the next foot. So all you really need to know is if (inches > 0)

You also didn't say where this code was placed, which is critical to the solution. I'll assume that it is a Calculation script in your hidden field.

Try this:

var feet = this.getField("feet").value;

var inches = this.getField("inches").value;

if(inches > 0)

event.value = feet+1;

else

event.value = feet;

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Report

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

Copy link to clipboard

Copied

Sorry Thom, seems like I was typing at the same time as you.

Likes

Report

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

Copy link to clipboard

Copied

LATEST

No worries.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Report

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