How to Fix Negative Subtraction Results in PDF Form JavaScript
Question details
The user is receiving a mathematically correct but functionally unexpected negative number when performing subtraction via JavaScript in a PDF form.

- Product
- Device & OS
- not provided
- Scenario
- Calculating numeric values using custom JavaScript inside interactive PDF form fields.
- Observed behavior
- Subtracting 800,000 from 100,000 returns -700,000, which functions as expected mathematically, but the user expects a positive absolute difference.
Verify the exact variable names assigned to your specific form fields before adjusting the calculation script.
Reverse the Operands in Your JavaScript Code
Adjust the subtraction formula to subtract the smaller variable from the larger one, ensuring a positive result.
The JavaScript calculation engine processes mathematics literally. If your script subtracts a large number from a smaller number (e.g., 100,000 - 800,000), it will always return a negative value. Reversing the operands resolves this logic error.
Right-click the target form field where the result is displayed and select 'Properties'.
Go to the 'Calculate' tab located at the top of the Properties dialog box.
Select 'Custom calculation script' and click 'Edit'. Locate the subtraction line (e.g., event.value = a - b;).
Change the order of the variables to subtract the smaller value from the larger one by updating the code to event.value = b - a;, then save and close.

Use Math.abs() to Force a Positive Difference
Wrap your subtraction calculation in the JavaScript absolute value function to always return a positive number regardless of the operand order.
Create and Calculate PDF Forms Using WPS Office
WPS Office provides advanced yet accessible PDF editing tools, making it easy to create interactive forms, define field properties, and implement custom calculation scripts for accurate data processing.
- 1. Open your document: Launch WPS Office and open your interactive PDF document.
- 2. Access form tools: Navigate to the 'Form' tab on the top ribbon to reveal the interactive form editing tools.
- 3. Open field properties: Double-click the desired form field to open its Properties dialog and select the 'Calculate' tab.
- 4. Add custom script: Choose 'Custom calculation script', enter your optimized JavaScript formula, and apply the changes.

Frequently Asked Questions
Why does my PDF form calculation show a negative number?
A negative number appears when a larger value is subtracted from a smaller value (e.g., 100 - 800). To fix this, you must either reverse the variable order in your JavaScript formula or use the Math.abs() function.
How do I access JavaScript calculations in a PDF form?
Right-click the specific form field, select Properties, go to the Calculate tab, and choose the 'Custom calculation script' option to view or edit the underlying JavaScript code.
Does WPS Office support JavaScript in PDF forms?
Yes, WPS Office Pro supports viewing, editing, and executing JavaScript for interactive form calculations, field formatting, and data validation.




