How to Display Zero When a PDF Calculation Result Is Negative
Question details
The user needs a proper JavaScript snippet to subtract two PDF form fields and ensure the result defaults to zero if the calculation yields a negative number.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Setting up custom calculation scripts within a PDF form's field properties.
- Observed behavior
- Without the correct conditional statement, the form field either displays a negative number or throws an Acrobat JavaScript syntax error.
Ensure you are using a PDF editor that supports custom JavaScript for form fields, and note down the exact names of the input fields you are trying to calculate.
Use an If/Else Statement in Custom Calculation Script
Applying a valid JavaScript if/else statement ensures the result correctly evaluates the calculation and assigns zero if the value falls below zero.
To achieve conditional formatting in PDF calculations, you cannot use simplified field notation. You must use a custom calculation script that retrieves the field values, performs the subtraction, and uses an if/else block to determine the final output.
Right-click the target form field where you want the result to appear and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Calculate' tab, then select the 'Custom calculation script' radio button.
Click the 'Edit' button and paste the following script: var v1 = Number(this.getField('WeeklyHours').valueAsString); var v2 = Number(this.getField('StartingBonusHours').valueAsString); var total = v1 - v2; if (total < 0) event.value = 0; else event.value = total;
Click 'OK' to save the script, close the Properties dialog, and enter test values into your form to verify the calculation behaves as expected.

Manage and Edit PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to manage form fields, edit text, and handle interactive PDF documents effortlessly.
- 1. Open Your PDF: Launch WPS Office and open your PDF document containing the interactive form fields.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all available form editing and creation tools.
- 3. Modify Field Properties: Double-click on any text field to open its properties menu and adjust calculation or formatting settings.
- 4. Save Your Changes: Once your form fields are properly configured, click the 'Save' icon to apply your modifications to the PDF.

Frequently Asked Questions
Why is my PDF custom calculation script failing?
Custom scripts often fail due to syntax errors, incorrect capitalization of variables, or mismatched field names. Verify that the names in your this.getField('FieldName') commands match your actual PDF fields exactly.
Can I use Simplified Field Notation for conditional logic?
No, Simplified Field Notation only supports basic mathematical operations (like addition or subtraction). To return zero for negative numbers, you must use a Custom Calculation Script with JavaScript conditional statements.
How do I test my PDF form calculation?
Exit 'Prepare Form' or editing mode to return to standard viewing mode. Enter a combination of numbers in the source fields to ensure the calculated field properly updates to zero when the subtraction results in a negative number.




