PDF Editing Issues

How to Fix Negative Subtraction Results in PDF Form JavaScript

Chanuka GeekiyanageChanuka Geekiyanage Nov 3, 2026 869 views

Question details

The user is receiving a mathematically correct but functionally unexpected negative number when performing subtraction via JavaScript in a PDF form.

How to Fix Negative Subtraction Results in PDF Form JavaScript
Product
PDF
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.
Before you start

Verify the exact variable names assigned to your specific form fields before adjusting the calculation script.

Solution 1Recommended

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.

1
Open field properties

Right-click the target form field where the result is displayed and select 'Properties'.

2
Navigate to calculation settings

Go to the 'Calculate' tab located at the top of the Properties dialog box.

3
Edit the script

Select 'Custom calculation script' and click 'Edit'. Locate the subtraction line (e.g., event.value = a - b;).

4
Reverse variables

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.

Reverse the Operands in Your JavaScript Code
Mathematical Logic: 100,000 minus 800,000 is mathematically -700,000. Changing the formula to 800,000 minus 100,000 yields the positive 700,000 you expect.
Advanced PDF Form Editing

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. 1. Open your document: Launch WPS Office and open your interactive PDF document.
  2. 2. Access form tools: Navigate to the 'Form' tab on the top ribbon to reveal the interactive form editing tools.
  3. 3. Open field properties: Double-click the desired form field to open its Properties dialog and select the 'Calculate' tab.
  4. 4. Add custom script: Choose 'Custom calculation script', enter your optimized JavaScript formula, and apply the changes.
Easily insert text fields, checkboxes, and custom calculation fields.Support for custom JavaScript to handle complex PDF form calculations.Highly compatible with standard PDF formats and Adobe Acrobat interactive forms.Lightweight and user-friendly interface for seamless PDF editing and form creation.
microsoft office alternative - wps office

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.