How to Apply Conditional Decimal Formatting in PDF Forms with JavaScript
Question details
Dynamically adjust the number of decimal places displayed in a PDF text field based on a user's selection from a dropdown menu.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where numeric formatting needs to change depending on a dropdown selection, such as switching between 2 or 4 decimal places based on a contract type.
- Observed behavior
- The user needs to correctly script the format or calculate event of the target field to read the dropdown value and format the numeric output accordingly.
Ensure you have a professional PDF editor that supports form field creation and custom JavaScript calculations, and verify the exact names of your target text field and dropdown field.
Use a Custom JavaScript Format Script
Apply a custom JavaScript formatting script to the target text field to read the dropdown value and format the numeric value conditionally.
By utilizing the util.printf function in combination with a conditional statement, you can alter how numbers are displayed without changing the underlying value. This is highly useful for forms handling multiple currencies or precise measurement formats.
Open your PDF document in your PDF editor, enter the Form Preparation or Edit mode, and right-click your target text field to select 'Properties'.
In the Field Properties dialog box, go to the 'Format' tab and select 'Custom' from the Select format category dropdown.
Click the 'Edit' button next to the Custom Format Script and enter the following code: `var drop = this.getField("Contract type").valueAsString; if(drop == "2") event.value = util.printf("%.2f", event.value); else if(drop == "4") event.value = util.printf("%.4f", event.value);`.
Click 'OK' to save the script, close the properties dialog, and test the form by entering a number and selecting different options in the 'Contract type' dropdown.

Edit and Manage Interactive PDF Forms with WPS Office
WPS Office offers a comprehensive suite of PDF tools, allowing you to seamlessly view, fill, and manage interactive PDF forms. Whether you need to input data, add digital signatures, or convert forms to Excel for data analysis, WPS has you covered.
- 1. Open your PDF: Launch WPS Office and open the interactive PDF form.
- 2. Access Form Tools: Navigate to the 'Form' or 'Fill & Sign' tab to access interactive fields.
- 3. Fill and Save: Interact with dropdowns and text fields smoothly, then save or export your completed document.

Frequently Asked Questions
Why is the decimal format not updating immediately when I change the dropdown?
The text field might need to lose focus or a recalculation event needs to be triggered for the JavaScript to execute. Ensure your dropdown field is set to 'Commit selected value immediately' in its properties.
Can I use this JavaScript format for currency symbols as well?
Yes, you can modify the util.printf function string to include text or currency symbols. For example, you can use `util.printf("$%.2f", event.value)` to display a dollar sign alongside the two decimal places.
What happens if the dropdown value doesn't match the script conditions?
If the JavaScript doesn't include an 'else' statement to handle unmatched conditions, the text field will either display its unformatted raw value or retain the last applied format. It is best practice to include a default formatting fallback in your script.




