Skip to main content
Participating Frequently
December 6, 2016
Answered

Round to whole number

  • December 6, 2016
  • 1 reply
  • 1374 views

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.

This topic has been closed for replies.
Correct answer try67

What?


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;

}

1 reply

try67
Community Expert
Community Expert
December 7, 2016

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;

Participating Frequently
December 7, 2016

When put that in the field is blank

try67
Community Expert
Community Expert
December 7, 2016

What?