How to Handle Variable Decimal Places in Acrobat PDF JavaScript
Question details
The user needs to dynamically format a numerical form field to either 2 or 4 decimal places depending on the value selected in a dropdown menu.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Setting up interactive PDF form calculations where precision requirements change based on the selected contract type.
- Observed behavior
- Requires a script to properly retrieve form values, apply conditional decimal formatting with JavaScript, and perform logical comparisons without syntax errors.
Verify that all relevant form fields (dropdowns and numerical input fields) are already created in your PDF and note their exact field names, as JavaScript is case-sensitive.
Use JavaScript .toFixed() for Conditional Decimal Formatting
Retrieve form field data as strings, convert them into numbers, and apply the .toFixed() method within conditional logic to enforce decimal precision.
In Adobe Acrobat, form field data is usually handled as text strings. To manipulate this data mathematically or format decimal places, you must cast the text into a number using the Number() function.
When comparing formatted numbers (which are converted back to strings by .toFixed()), you must wrap them in Number() again to ensure logical operators function correctly.
Open your PDF in form editing mode, right-click the field where you want the final result to appear, and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab in the Properties dialog box, select the 'Custom calculation script' option, and click the 'Edit' button.
Define your variables by fetching the data from your dropdown and input fields. Make sure to append '.value' to the method, like: var drop = this.getField('Contract type').value;
Write an if/else statement referencing your dropdown variable. Inside the conditions, convert your raw input to a number and format it using the .toFixed() method (e.g., RY = Number(rawRY.toFixed(2)); or .toFixed(4)).
Perform your final comparison using the newly formatted variables (e.g., if (RY > CYP)). Assign your final output text or number to 'event.value' and click 'OK' to save your script.

Create Interactive PDF Forms with WPS Office
WPS Office features a robust PDF editor that allows you to effortlessly design interactive forms, insert form controls, and manage calculation properties.
- 1. Open WPS PDF: Launch WPS Office and open your PDF document within the WPS PDF tool.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view all available form creation tools.
- 3. Insert Interactive Fields: Use the toolbar to click and drag Text Fields, Combo Boxes, or Checkboxes onto your document layout.
- 4. Configure Properties: Double-click any inserted form field to open its properties panel, where you can define formatting, appearance, and standard calculations.

Frequently Asked Questions
Why is my PDF calculation script returning an error or remaining blank?
Errors usually occur if a field name is misspelled or if the '.value' property is missing when fetching a field's data. Verify that your field names in the script match the actual PDF field names exactly, including capitalization.
Can I format a PDF text field to always show two decimal places without using JavaScript?
Yes. If the decimal places do not need to change dynamically, you can simply right-click the field, go to 'Properties', select the 'Format' tab, choose 'Number' as the format category, and set 'Decimal Places' to 2.
What does the Number() function do in PDF JavaScript?
PDF form fields natively store input as text strings. The Number() function converts these strings into actual mathematical numbers so that operations like addition, division, or greater-than/less-than comparisons calculate correctly.
How do I test my custom calculation script in the PDF?
Close the form editing mode and select the standard Hand tool or selection tool. Change your dropdown selections and input test numbers to verify that the output field updates dynamically according to your script's logic.




