PDF Editing Issues

How to Apply Conditional Decimal Formatting in PDF Forms with JavaScript

Steven ParkerSteven Parker Nov 24, 2026 868 views

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.

Apply Conditional Decimal Formatting in PDF Forms with JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Form Edit Mode

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'.

2
Navigate to the Format Tab

In the Field Properties dialog box, go to the 'Format' tab and select 'Custom' from the Select format category dropdown.

3
Enter the JavaScript Code

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);`.

4
Save and Test

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.

Use a Custom JavaScript Format Script
Dropdown Configuration: Ensure that your dropdown field has the 'Commit selected value immediately' option checked in its options tab so the text field updates instantly when the selection changes.
Advanced PDF Form Management

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. 1. Open your PDF: Launch WPS Office and open the interactive PDF form.
  2. 2. Access Form Tools: Navigate to the 'Form' or 'Fill & Sign' tab to access interactive fields.
  3. 3. Fill and Save: Interact with dropdowns and text fields smoothly, then save or export your completed document.
Effortlessly fill out interactive and JavaScript-enabled PDF formsHighly compatible with Adobe Acrobat PDF formats and standardsEasily convert PDF form data to Excel or Word for quick reportingLightweight, fast document loading, and a user-friendly interface
microsoft office alternative - wps office

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.