Copy link to clipboard
Copied
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
1 Correct answer
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I used the second script - works perfectly!!! Thank you so much !
Kim
Copy link to clipboard
Copied
Can you help me with the same thing to round up or round down same as her question. .5 round up and anything .4 round down. I'm stuck.
Blue is the box that I need to put the script to round up/round down. I took a variable that was on Youtube but I don't think this is correct for my formula/text box. Any assistance is greatly appreciated. Thank you!
Copy link to clipboard
Copied
For context:
I am adding HRS1+HRS2+ HRS3+HRS4 = HrsTotal1
Blue Box (Ave1) is the value that I need it to round up/down
so I am taking
HrsTotal1 /Drop2 = value ( I need this to round up/down)
Copy link to clipboard
Copied
To round, use Math.round(), try this:
var a = Number(this.getField("HrsTotal1").valueAsString);
var b = Number(this.getField("Drop2").valueAsString);
if(b)
event.value = Math.round(a/b);
else
event.value = 0;
Copy link to clipboard
Copied
Thank you so much! This worked, you saved me from a huge headache.
Copy link to clipboard
Copied
Nesa Nurani, can you help me again with another complex javascript?
I'm willing to pay for your service. I'd love to hear back from you.
I've been struggling with a JavaScript issue that I couldn't resolve. As a temporary solution, I've created small text boxes with their own math, but I understand that this is not a professional approach.
My HRS per WK column is my goal.
1.) Have all the hours added up show in the HrsTotal1 box which I was able to do with the simple sum box.
I need help with step 2
2.) Be able to divide that by 4, (divide by 3 and then divide by 2 again), divide by 2...so I can get the average and round up .5 or down .4
example: If the total was 285
285 / 4
285 / 3 / 2
285 / 2
My email is nikkivue85@gmail.com. I'd love to hear from you. Thanks!

