Skip to main content
Inspiring
February 22, 2023
Answered

How to format a integer fields to Currency?

  • February 22, 2023
  • 1 reply
  • 3653 views

The requirement is like, Users will enter their salary, no alpa, just numbers. After finishing type, the numbers converted to currency formatting.

E.g.  1234 --> $1,234.00

Can someone help on the script on the data transformer and also need to limit user enter any other charactors but only numbers.

Note: the Salary Field is a String field.

This is where I got so far, but it doesn't work..

 

var salaryScript = "";
salaryScript = "var salary = getField("Salary");";
salaryScript += "var salaryEntered = salary.value;";
salaryScript += "var currency = '$' + parseFloat(salaryEntered).toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');";
salaryScript += "salary.value = currency;";

var salaryField = getField("Salary");
salaryField.setAction("OnBlur", salaryScript );

This topic has been closed for replies.
Correct answer try67

Correct it -- No, we are NOT allowed do any modify on the Acorbat. All the work need to be done on the 3rd part design tool, then genereate a correct PDF Form for customer to use.


This is all you need:

 

var salaryField = this.getField("Salary");
salaryField.setAction("Format", "event.value = util.printf(\"$%,0.2f\",event.value);");

1 reply

Thom Parker
Community Expert
Community Expert
February 22, 2023

Your script is way over complicated and problematic. Do not use it. 

Why not use the built-in currency number formatting instead?

 

The alternative is to use a custom format script.  

Like this:

 

event.value = util.printf("$%,0.2f",event.value);

 

You should learn a bit about how field event work in Acrobat Specifically Format events.

Here's an article on the topic:

https://www.pdfscripting.com/public/Formatting-Form-Fields.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
February 22, 2023

Thanks for your reply. I am a start leaner of JS. And I am working on a 3rd part design tool which support JS. 

I noticed the 'event.value = util.printf("$%,0.2f",event.value);' somewhere. But I don't know how to use it or how to connect with the field 'Salary' I already defined. 

 

In the design tool, I can define it as any type of below, I chose dobule, as a result, it shows up 2000.00 but no comma and $ symbol. So I need to do with script to format the field Salary, but I how to connet the code you posted with this Filed. 

 

 

try67
Community Expert
Community Expert
February 22, 2023

Use Acrobat to do it. There you will be able to simply select the Currency option under the Format tab and it will take care of the code for you. No need to re-invent the wheel...