PDF Editing Issues

How to Round Down Calculated PDF Form Values Using JavaScript

John WilsonJohn Wilson Nov 3, 2026 869 views

Question details

The user needs a calculated PDF form field to display only the whole-number portion of a result, dropping any decimals without rounding up, and requires the script to recalculate automatically when the source field changes.

How to Round Down Calculated PDF Form Values Using JavaScript
Product
PDF Forms
Device & OS
not provided
Scenario
Writing custom calculation scripts for interactive PDF form fields to process numerical values dynamically.
Observed behavior
The current calculation script fails to drop decimal points appropriately (e.g., leaving values at 4.99 instead of 4) and does not trigger a recalculation when the driving source field's value is modified.
Before you start

Verify the exact name of your source data field (e.g., 'ModuleWidth') and ensure you have access to the calculation order settings in your PDF form editor.

Solution 1Recommended

Use Math.floor and Verify Calculation Order

Implement a custom JavaScript using Math.floor to force the result to round down to the nearest whole number, and correct the form's calculation sequence.

To ensure a calculated field only displays the whole number portion, standard formatting is insufficient as it often rounds up (e.g., 4.99 becomes 5). Using the JavaScript Math.floor() method directly modifies the value.

Additionally, if a calculated field does not update when a dependent field changes, the document's calculation order is likely incorrect. The driving field must be calculated before the result field.

1
Enter the Custom Calculation Script

Right-click the result field in form editing mode, select 'Properties', and navigate to the 'Calculate' tab. Choose 'Custom calculation script' and enter the following code: var result = (Number(this.getField("ModuleWidth").valueAsString) - 12) / 180; event.value = Math.floor(Math.abs(result));

2
Apply valueAsString to Source Fields

Ensure you use the 'valueAsString' property instead of 'value' when referencing the source field. This prevents JavaScript from interpreting empty fields or strings improperly during mathematical operations.

3
Adjust the Calculation Order

Close the field properties. In your PDF form editor toolbar, find the 'Calculation Order' or 'Set Field Calculation Order' option. Move your source fields higher in the list so they are calculated before the final result field.

Use Math.floor and Verify Calculation Order
Handling Absolute Values: Wrapping the result in Math.abs() prior to Math.floor() ensures that the script correctly handles both positive and negative values without unexpected downward shifts on negative numbers.
Advanced PDF Form Management

Build and Calculate Complex PDF Forms with WPS Office

WPS Office provides robust PDF editing capabilities, enabling you to effortlessly insert interactive fields, configure calculation sequences, and apply custom JavaScript functions for accurate data processing.

  1. 1. Open the Form Builder: Launch WPS Office and open your PDF document. Navigate to the 'Form' tab to access interactive form controls.
  2. 2. Configure Field Properties: Double-click the target text field to open its properties, then navigate to the 'Calculate' tab to input your custom JavaScript code.
  3. 3. Test the Calculation: Exit form editing mode and input sample data into your source fields to verify that the mathematical rounding and calculations perform correctly.
Full support for standard PDF JavaScript actionsSeamless compatibility with Adobe Acrobat PDF formsIntuitive interface for managing field calculation orderLightweight, fast, and feature-rich PDF editor
microsoft office alternative - wps office

Frequently Asked Questions

Why isn't my PDF form recalculating when the source field changes?

This happens when the document's calculation order is incorrect. If the final result field evaluates before the source field updates, the old value is used. You must edit the PDF's Calculation Order so the source field is calculated first.

What is the difference between Math.round and Math.floor in PDF JavaScript?

Math.round() rounds a number to the nearest integer, meaning 4.99 becomes 5. Math.floor() always rounds down to the nearest integer, so 4.99 becomes 4, effectively dropping the decimals.

How does Math.floor handle negative numbers in PDF calculations?

Math.floor() rounds towards negative infinity. For example, Math.floor(-4.01) evaluates to -5. If you strictly want to drop decimals from negative numbers (resulting in -4), you should use Math.trunc() or apply Math.abs() logic depending on your exact requirements.

Can I remove decimals using the built-in number formatting instead of JavaScript?

Using built-in number formatting with zero decimal places will visually hide the decimals, but it automatically rounds the number to the nearest whole integer (standard rounding). To strictly round down and drop decimals, custom JavaScript is required.