Skip to main content
kimkay01
New Participant
August 1, 2016
Answered

Help with a custom calculation to round up/down in Adobe Pro

  • August 1, 2016
  • 1 reply
  • 3090 views

Hello!  I've never written a custom calculation, so I'm lost; could someone please help me write one to round up/down at .50/.49 with this field name:

CreditedTotalLocal+OtherHE

For example, if the value of this field - which is the result of the "Value is the sum(+) of the following fields" function on two fields named CreditedXXXXLocalHE and CreditedOtherHE - is between 3.0 and 3.49, this field should be 3.0.  If the value is 3.50 to 3.99, the field should show 4.0.

I'm sure this is very simple, but I've tried some things from this and other forums with no luck.  Please help!

Thanks in advance,

Kim

This topic has been closed for replies.
Correct answer gkaiseril

Instead of using the "Field is the sum of the following fields:" or the Simplified "Field Notation" you need a custom calculation script like

function myRound (n, d)
{
/* By D. P. Story
http://math.uakron.edu/~dpstory/acrotex/elementary/rounding.pdf
*/
    n = String(n).replace(/^,$/g,"");
    d = -1*d
    var m = Math.pow(10,d);
    n *= m;
    n = Math.round(n);
    n /= m;
    d = ( d > 0 ) ? d : 0;
    n = n.toFixed(d);
    return n;
}

var nResult = this.getField("CreditedXXXXLocalHE").value + this.getField("CreditedOtherHE").value;
event.value = myRound(nResult, 0);

or

function DoubleRound(nValue, nDec)
{
var nResult =  Math.round(nValue * Math.pow(10, nDec + 1)) / Math.pow(10, nDec + 1);
return Math.round(nResult * Math.pow(10, nDec)) / Math.pow(10, nDec);
}

var nResult = this.getField("CreditedXXXXLocalHE").value + this.getField("CreditedOtherHE").value;
event.value = DoubleRound(nResult, 0);

Both the above functions can round to decimal values, myRound use -Dec for the decimal value to round to while DobuleRound use the positive value for the decimal number or to powers of 10 like the 1,000 using the 3 for myRound and -3 for DoubleRound.

1 reply

Inspiring
August 2, 2016

If you only need the displayed value of the calculation rounded to 0 decimal places, on the "Format" tab for the field set the number of decimal places to "0".

If you need the actual computed value rounded to 0 decimal places then you will have to use the Custom JavaScript Calculation option. This situation usually occurs when computing taxes, some statistical calculations, and when computing values near zero and comparing the results to zero. The issue is caused by how decimal values are handled in the PDFs.

kimkay01
kimkay01Author
New Participant
August 2, 2016

I need the actual computed value to round up from 3.50, down from 3.49.  And I would like zero decimal places, or one (as in 3 or 3.0) for the result.  I know a custom calculation is needed, I just don't know how to write one .

Thanks!

Kim

gkaiserilCorrect answer
Inspiring
August 2, 2016

Instead of using the "Field is the sum of the following fields:" or the Simplified "Field Notation" you need a custom calculation script like

function myRound (n, d)
{
/* By D. P. Story
http://math.uakron.edu/~dpstory/acrotex/elementary/rounding.pdf
*/
    n = String(n).replace(/^,$/g,"");
    d = -1*d
    var m = Math.pow(10,d);
    n *= m;
    n = Math.round(n);
    n /= m;
    d = ( d > 0 ) ? d : 0;
    n = n.toFixed(d);
    return n;
}

var nResult = this.getField("CreditedXXXXLocalHE").value + this.getField("CreditedOtherHE").value;
event.value = myRound(nResult, 0);

or

function DoubleRound(nValue, nDec)
{
var nResult =  Math.round(nValue * Math.pow(10, nDec + 1)) / Math.pow(10, nDec + 1);
return Math.round(nResult * Math.pow(10, nDec)) / Math.pow(10, nDec);
}

var nResult = this.getField("CreditedXXXXLocalHE").value + this.getField("CreditedOtherHE").value;
event.value = DoubleRound(nResult, 0);

Both the above functions can round to decimal values, myRound use -Dec for the decimal value to round to while DobuleRound use the positive value for the decimal number or to powers of 10 like the 1,000 using the 3 for myRound and -3 for DoubleRound.