How to Fix Acrobat JavaScript Concatenating Numbers in PDF Forms
Question details
The user needs to prevent custom JavaScript calculation scripts in PDF forms from combining (concatenating) numbers as text strings.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Adding multiple form field values together using custom calculation scripts.
- Observed behavior
- When some fields are blank or treated as text strings, the script concatenates the values (e.g., producing 888 instead of 24) instead of performing mathematical addition.
Before modifying your form calculations, check your form field names for typos and ensure you have access to edit the custom JavaScript properties.
Explicitly Convert String Values to Numbers in JavaScript
Use the Number() function in your custom script to force Acrobat to treat field values as integers rather than text strings, preventing concatenation.
By default, Acrobat may treat empty or specific field values as text strings. In JavaScript, using the plus operator on strings results in concatenation rather than addition. Wrapping the value retrieval in a Number() conversion resolves this behavior.
Open your PDF form in Adobe Acrobat, enter 'Prepare Form' mode, right-click the calculation field, and select 'Properties'.
Navigate to the 'Calculate' tab, ensure 'Custom calculation script' is selected, and click the 'Edit' button.
Modify your script to wrap the field value in the Number() function. For example: res += Number(this.getField('YourFieldName').valueAsString);
Add a conditional statement to leave the final output blank if the result is 0. For example: if (res == 0) { event.value = ''; } else { event.value = res; }

Re-create Fields from a Working Script
Resolve underlying formatting issues caused by importing documents from Excel by replacing problematic fields.
Easily Calculate PDF Form Fields with WPS PDF
Skip complex JavaScript coding. Set up automatic field calculations like sums, products, or averages directly using WPS PDF's intuitive form properties.
- 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document containing the form fields.
- 2. Access Form Controls: Navigate to the 'Form' tab on the top ribbon to view all interactive field options.
- 3. Open Field Properties: Double-click the total field where you want the result to appear to open its Properties dialog.
- 4. Set Calculation Formula: Go to the 'Calculate' tab, select 'Value is the [Sum] of the following fields', and check the boxes for the fields you wish to add together.

Frequently Asked Questions
Why does JavaScript use concatenation instead of addition in my PDF?
In JavaScript, the plus operator is used for both mathematical addition and string concatenation. If any of the field variables are retrieved as text (strings) rather than numbers, the script defaults to linking the text together.
How do I handle blank fields in Acrobat form calculations?
Blank fields often return an empty string. You can use Number(this.getField('FieldName').valueAsString) to safely convert an empty string into a mathematical zero, preventing concatenation errors.
Can I calculate PDF fields without writing custom JavaScript?
Yes. In most PDF editors, you can open the text field properties, navigate to the Calculate tab, and choose predefined operations like Sum, Product, Average, Minimum, or Maximum without writing any custom code.




