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

How to format a integer fields to Currency?

Explorer ,
Feb 21, 2023 Feb 21, 2023

Copy link to clipboard

Copied

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

Views

2.2K

Translate

Translate

Report

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

correct answers 1 Correct answer

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

Votes

Translate

Translate
Community Expert ,
Feb 21, 2023 Feb 21, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.png323.png

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

This is all you need:

 

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Thanks for your support! It works well. 

Votes

Translate

Translate

Report

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