Keep a PDF Form Calculation Positive with JavaScript in Adobe Acrobat
Question details
The user wants a PDF form calculation that combines field values and keeps the displayed result nonnegative.

- Product
- PDF forms with JavaScript, such as Adobe Acrobat Pro
- Device & OS
- not provided
- Scenario
- A calculated form field needs to combine several numeric field values and handle a negative total according to a chosen rule.
- Observed behavior
- The desired result is an absolute value, zero, or another defined response when the combined total is negative.
Decide whether a negative total should become its absolute value, become zero, or trigger an error. Open the form in a PDF editor that supports custom form calculation scripts, and identify the exact names of the numeric input fields.
Use an Absolute-Value Calculation in Adobe Acrobat Pro
Use this method when a negative sum should display as its positive magnitude.
Math.abs() changes a negative total to its positive magnitude after the fields are summed. A total of zero remains zero, so this produces a nonnegative result rather than guaranteeing a value greater than zero.
Open the PDF in Adobe Acrobat Pro and choose All tools > Prepare a form.
Double-click the field that should display the result, then select the Calculate tab.
Select Custom calculation script and click Edit. Enter the script below, replacing Field1, Field2, and Field3 with your exact field names: var a = Number(this.getField("Field1").value) || 0; var b = Number(this.getField("Field2").value) || 0; var c = Number(this.getField("Field3").value) || 0; event.value = Math.abs(a + b + c);
Close the field properties, select Preview, and enter values that produce positive, negative, and zero totals. Save the PDF after confirming the results.

Display Zero When the PDF Form Total Is Negative
Use this alternative when negative totals should be clamped to zero rather than converted to their absolute values.
Work with PDFs and Office Files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Microsoft Office files. Its format compatibility helps you move existing documents into WPS, though it does not replace Acrobat's custom PDF form calculation tools.

Frequently Asked Questions
What is the difference between Math.abs() and Math.max(0, total)?
Math.abs(total) converts a negative total to its positive magnitude. Math.max(0, total) displays zero for any negative total while leaving positive totals unchanged.
Can the calculation return a strictly positive value?
Math.abs() and Math.max(0, total) can both return zero. If zero is not allowed, define a separate rule for it, such as displaying an error or requiring a minimum value greater than zero.
Where do I find the custom calculation script setting?
In Adobe Acrobat Pro, choose All tools > Prepare a form, double-click the result field, and select Calculate > Custom calculation script > Edit.
Why does the calculation not work in every PDF viewer?
PDF viewers differ in their support for form JavaScript. Test the saved form in the viewer recipients will use; a viewer that does not run the calculation script may not update the result.




