PDF Editing Issues

How to Display Zero When a PDF Calculation Result Is Negative

John WilsonJohn Wilson Nov 5, 2026 869 views

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.

How to Display Zero When a PDF Calculation Result Is Negative
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Form Field Properties

Right-click the target form field where you want the result to appear and select 'Properties' from the context menu.

2
Navigate to the Calculate Tab

In the Properties dialog box, click on the 'Calculate' tab, then select the 'Custom calculation script' radio button.

3
Enter the JavaScript Code

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;

4
Save and Test

Click 'OK' to save the script, close the Properties dialog, and enter test values into your form to verify the calculation behaves as expected.

Use an If/Else Statement in Custom Calculation Script
Update Field Names: Make sure to replace 'WeeklyHours' and 'StartingBonusHours' in the script with the exact field names used in your specific PDF document.
Advanced PDF Editing

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. 1. Open Your PDF: Launch WPS Office and open your PDF document containing the interactive form fields.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all available form editing and creation tools.
  3. 3. Modify Field Properties: Double-click on any text field to open its properties menu and adjust calculation or formatting settings.
  4. 4. Save Your Changes: Once your form fields are properly configured, click the 'Save' icon to apply your modifications to the PDF.
Easily create, fill, and manage interactive PDF forms.Highly compatible with standard Adobe Acrobat PDF formats.Fully compatible with Microsoft Office Word, Excel, and PowerPoint files.Lightweight application with a seamless and user-friendly interface.
microsoft office alternative - wps office

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.