14 Replies Latest reply on Dec 7, 2016 12:26 PM by try67

# Round to whole number

I am using Adobe Pro XI. Needing a javascript

I am trying to get amount authorized.  I need to take the value is in field distance divide by 350.  Then take the whole number times it by the whole number.  Then the field distance value-350 to see if it is greater than or less than 50.  If it is equal to or less than 50 use the whole number if it is greater than 50 then use the whole number plus 1.  If the distance is 350 or less then would be 1.  I have no problem in excel doing it but I have not figure out how to get it all to work using the JAVAScripts.

375/350=1.071

1*350=350

375-350=25

is 25 > 50 =no

returned value would be 1

751/350=2.145

2*350=700

751-350=51

is 51> 50 = yes

2 + 1 = 3

Returned value would be 3.

• ###### 1. Re: Round to whole number

Your description is incorrect. The third line in the second example should probably be:

751-700=51

Not:

751-350=51

I think this code should do the trick, though. It's the custom calculation script for the field that should contain the final result:

```var distance = Number(this.getField("Distance").value);
var v1 = Math.floor(distance / 350);
var v2 = v1 * 350;
var v3 = distance - v2;
if (v3<50) event.value = v1;
else event.value = v1 + 1;
```
• ###### 2. Re: Round to whole number

When put that in the field is blank

• ###### 3. Re: Round to whole number

What?

• ###### 4. Re: Round to whole number

Do you mean if the "Distance" field is blank? Assuming that you want the result to also be blank, use this code:

```if (this.getField("Distance").valueAsString=="") {
event.value = "";
} else {
var distance = Number(this.getField("Distance").value);
var v1 = Math.floor(distance / 350);
var v2 = v1 * 350;
var v3 = distance - v2;
if (v3<50) event.value = v1;
else event.value = v1 + 1;
}
```
• ###### 5. Re: Round to whole number

I put 350 or what ever number in the location the field where I want the answer is stays blank.  No matter what I put in the "distance" field the "AT" field stays blank.

• ###### 6. Re: Round to whole number

Press Ctrl+J and check the console for error messages.

• ###### 7. Re: Round to whole number

ReferenceError: p1 is not defined

7:Field:Calculate

TypeError: this.getfield is not a function

4:Field:Calculate

• ###### 9. Re: Round to whole number

I only have the error:

ReferenceError: p1 is not defined

7:Field:Calculate

I do not see where it is talking about p1

• ###### 10. Re: Round to whole number

You didn't copy my code carefully enough.

JS is case-sensitive. The name of the function I used is getField, not getfield...

• ###### 11. Re: Round to whole number

It is working now thank you for your help for that.

There is one other problem that I cannot get to work.  If the "distance" is < 51 then should return a 1.  Works right if "distance" is >= 51.

have tried a few things and still cannot get it to work.

• ###### 12. Re: Round to whole number

Change line #5 to:

if (v3<51) event.value = v1;

• ###### 13. Re: Round to whole number

I have that for line #5.  but if the value is 50 or less its returning 0 when I need it to be 1 when the

If "distance" = 0 is the only time I need it to have 0.  If "distance" is = 1 to 50 still need it to be 1.

• ###### 14. Re: Round to whole number

I used the formula you provided. If you want different results then you need to use a different formula.