Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to format a integer fields to Currency?

Explorer ,
Feb 21, 2023 Feb 21, 2023

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

TOPICS
How to , JavaScript , PDF forms
3.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Feb 22, 2023 Feb 22, 2023

This is all you need:

 

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2023 Feb 21, 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2023 Feb 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. 

 

33.pngexpand image323.pngexpand image

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2023 Feb 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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2023 Feb 22, 2023

No, we are 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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2023 Feb 22, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2023 Feb 22, 2023

This is all you need:

 

var salaryField = this.getField("Salary");
salaryField.setAction("Format", "event.value = util.printf(\"$%,0.2f\",event.value);");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2023 Feb 22, 2023
LATEST

Thanks for your support! It works well. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines