How to Round Down Positive and Negative Calculations in PDF Forms
Question details
The user needs a custom calculation in a PDF form that rounds down results, preserves negative values, and handles empty input fields without returning errors or stripping negative signs.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Setting up custom JavaScript calculations for interactive fields in a PDF form.
- Observed behavior
- Using incorrect JavaScript functions like Math.abs() removes negative signs from results, and failing to check for empty source fields causes calculation errors or unwanted default zero values.
Ensure you have access to a PDF editor that supports custom JavaScript for form fields, and note the exact names of the input fields you intend to reference in your calculation.
Use Math.floor() with an Empty Field Check
Implement a custom JavaScript snippet that checks for empty values, calculates the formula, and uses Math.floor() to round down while preserving negative signs.
When calculating values in PDF forms, using Math.abs() inadvertently strips the negative sign from negative numbers. To round down correctly while preserving the sign, Math.floor() is the best approach.
Additionally, converting the input to a string first allows you to check if the field is empty, preventing the formula from calculating blank fields as zeros unless specifically instructed.
Open your PDF form in editing mode, right-click the destination field where the calculated result should appear, and select 'Properties'.
Navigate to the 'Calculate' tab, choose the 'Custom calculation script' option, and click the 'Edit' button.
Input the script to fetch the source field value as a string. For example: var value = this.getField('YourFieldName').valueAsString;
Add an if-else statement to handle empty fields to prevent errors: if (value == '') { event.value = '0'; } (You can also set event.value = '' if you want it to remain blank).
In the else block, calculate and round down the result: else { event.value = Math.floor((Number(value) - 12) / 180); } Replace the formula logic with your own specific calculation requirements.
Create and Calculate Interactive PDF Forms with WPS Office
WPS Office provides robust PDF editing tools, allowing you to create interactive forms, add text fields, and easily configure custom calculation scripts for accurate data processing.
- 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document. Navigate to the 'Form' tab on the top ribbon.
- 2. Insert Form Fields: Click 'Text Field' to add customizable input and result fields to your document layout.
- 3. Set Form Properties: Double-click the destination result field to open its properties, go to the Calculate tab, and enter your custom rounding JavaScript.

Frequently Asked Questions
Why does my PDF calculation turn negative numbers into positive ones?
This usually happens if your calculation script includes the Math.abs() function, which returns the absolute (positive) value of a number. Removing it will preserve your negative signs.
How do I prevent my PDF formula from showing '0' when the input field is empty?
You can add a conditional check in your JavaScript. For example, if (inputValue == '') { event.value = ''; } will keep the result field blank instead of displaying a zero.
What is the difference between Math.floor() and Math.round() in PDF form scripts?
Math.floor() always rounds a number down to the nearest integer (e.g., 2.9 becomes 2, and -2.1 becomes -3). Math.round() rounds to the nearest integer based on standard rounding rules (e.g., 2.5 becomes 3, and 2.4 becomes 2).




