23 Replies Latest reply on Feb 10, 2014 11:36 AM by csb102155

# Javascript division

Hello all, I use this script for calculating a division, but the answers are all over the place.  Can someone tell me what codes would work better?

var price = this.getField("design.1")

var qty = this.getField("Grand.1")

event.value = qty.value / price.value

Sometimes it calculates correctly, other times it doesn't.

I'm using Adobe Acrobat X Professional.

Thanks for the assistance.

• ###### 1. Re: Javascript division

You have to make sure that the denominator (the price field) is not zero (or blank) because division by zero is illegal.

• ###### 2. Re: Javascript division

Is there a better code that would work?  It’s like, taking 15 / 5 = 2.8 when it should be 3.

Connie

• ###### 3. Re: Javascript division

What is the format for your result field?

Do you want the display to show the result rounded to a given number of decimal palces and the computed value to be the raw result of the calculation?

Do you want the value of the calculation result to be rounded to a given number of decimal places?

Are you sure the input values are whole numbers?

var price = this.getField("design.1")

var qty = this.getField("Grand.1")

app.alert("design price: " + price.vlaue + "\nQuantity: " + qty.value + "\nResult quantity / pricce: " + (qty.value / price.value)));

event.value = qty.value / price.value

• ###### 4. Re: Javascript division

The format result is for example, 16 / 4 = 4.0.  The result should be one to the right of the decimal.

Connie

• ###### 5. Re: Javascript division

It sounds like the field calculation order could be incorrect since you're getting obviously incorrect results, but you should also address the division by zero problem as well.

• ###### 6. Re: Javascript division

So you are saying with a price of 16 and a quantity of 4 the division of the price by the  quantity is 4. That is correct.

Your script is dividing the quantity of 4 by the price of 16 which is 0.25.

• ###### 7. Re: Javascript division

Yes, 16 / 4 = 4, however, if I add another number, for example, 20 / 5, it comes out to 3.2 instead of 4.  The script I used just doesn’t seem to be stable and are calculating things wrong. Is there a better way to write the script so that it comes out exactly as it should?

Thanks

Connie

• ###### 8. Re: Javascript division

DId you check the field calculation order?

• ###### 10. Re: Javascript division

I’d be glad to set the form up to a link and share it.  How / where do I go to do this?

Connie

• ###### 11. Re: Javascript division

You can upload it to something like dropbox or acrobat.com and share the link here.

The issue I mentioned can be solved by adding something like this:

if (price.value==0) event.value = "";

else event.value = qty.value / price.value

• ###### 12. Re: Javascript division

I think I got the division problem solved, but now I can’t get the TotalPoints to total properly. Could someone take a look and see what I’m doing wrong?

Thanks

Connie Bretes

Program Manager

MDOT Graphics Design & Mapping

Forms Management

(517) 335-2520

• ###### 13. Re: Javascript division

What exactly is the issue with it?

Also, the way you implemented the fields in this form is not very good... When using a Likert scale the user expects the options to be mutually exclusive, ie when you select one the others (in the same group) should un-select.

You should use radio-buttons, not check-boxes.

• ###### 14. Re: Javascript division

The only issue I'm seeing is that you forgot to add total.5 to the list of fields that are summed up...

• ###### 15. Re: Javascript division

What we want to do with this is:

Total the 5’s (Excellent)

Total the 4’s (Above Average)

Total the 3’s (Satisfactory)

Total the 2’s (Unsatisfactory)

So, for example, If all five - 4’s are checked, it will total 20

Then we want the total number of each category selected

If 2-5’s were selected,

2-4’s were selected,

2-3’s were selected,

2-2’s selected and

2-1’s selected,

that would equal to 10 categories.

Then we want to take the 20 and divide it by 10.

We tried using the radio buttons, the problem was that if someone selected Not applicable, it still got counted in the category. We don’t want the Not Applicable to total in the category which was why I went to the check boxes.

If you can show me a better way to handle this, I’d appreciate it.  Thanks.

Connie Bretes

Program Manager

MDOT Graphics Design & Mapping

Forms Management

(517) 335-2520

• ###### 16. Re: Javascript division

The total works fine for me... I tick all five 4's and it shows 20.

Regarding the check-boxes: You can make them behave like radio-buttongs by giving them all the same name but different export values.

• ###### 17. Re: Javascript division

Yes, initially the totals will come up, but if you click one 5, one 4, one 3, one 2 and one 1, it totals to 14 instead of 15.

Connie Bretes

Program Manager

MDOT Graphics Design & Mapping

Forms Management

(517) 335-2520

• ###### 18. Re: Javascript division

I told you: You forgot to add total.5 to the sum...

• ###### 19. Re: Javascript division

Ok, I got that, I went in and cleaned it up and tried it again.  Initially, it works okay, but if the user decides they would rather change from i.e., satisfactory to unsatisfactory, the total doesn’t add up right.  Any suggestions?

Here’s the new file I’ve uploaded.

https://files.acrobat.com/preview/859c8524-49c1-4515-a714-d59f8b203c7e

Connie Bretes

Program Manager

MDOT Graphics Design & Mapping

Forms Management

(517) 335-2520

• ###### 20. Re: Javascript division

You need to make sure the field calculation order is correct, ie that the grand total is calculated after the sub-totals.

• ###### 21. Re: Javascript division

As far as I can tell, it is in calculation order.  The totals add up to the bottom of the rows first, and then Text19 totals, then Text20 totals and then Text21 calculates.  Am I missing something here?  By the way, thanks for your patience with me on this.  I find this rather frustrating.

Connie Bretes

Program Manager

MDOT Graphics Design & Mapping

Forms Management

(517) 335-2520

• ###### 22. Re: Javascript division

Did you check it under Form Edit mode, under Tasks - Other Tasks?

• ###### 23. Re: Javascript division

Thank you D Gilad – that was exactly what was wrong and it now calculates correctly for me.  Now to do all the rest of the form (sigh).

Connie Bretes

Program Manager

MDOT Graphics Design & Mapping

Forms Management

(517) 335-2520