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

Currency format in custom calculation script

Explorer ,
Feb 01, 2024 Feb 01, 2024

I have a long custom calculation script that combines 23 fields.  Below is a small portion of the results and a small portion of the script I’m using.  I have been successful with value.toFixed(2) to shorten to 2 decimal places.  I’ve been successful with the line splits using \n.

 

Now, I am trying to format the dollar amounts with a comma separating thousands and hundreds if applicable.

Any assistance would be helpful!

 

Portion of script:

event.value = "Traveler 1 Name: " + this.getField("Traveler1Name").value + "\nConference Registration  $ " + this.getField("ConfRegFeeT1").value.toFixed(2) +

"\nLodging  $ " + this.getField("HotelCostT1").value.toFixed(2) + " X " + this.getField("HotelNightsT1").value.toFixed(2) + " = $ " +  this.getField("HotelTotalT1").value. toFixed(2) +

"\nMeals:  Breakfast  $ " + this.getField("BreakfastCostT1").value.toFixed(2) + " X " + this.getField("BreakfastDaysT1").value.toFixed(2) + " = $ " + this.getField("BreakfastTotalT1").value.toFixed(2) +

"\nMeals:  Dinner $ " + this.getField("DinnerCostT1").value.toFixed(2) + " X " + this.getField("DinnerDaysT1").value.toFixed(2) + " = $ " +  this.getField("DinnerTotalT1").value.toFixed(2)

 

Results:

31578672014z_0-1706811082946.png

 

 

Any assistance would be helpful!

THANKS!

TOPICS
PDF forms
1.1K
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 01, 2024 Feb 01, 2024

The util.printf function returns a string. So it's used anywhere a formatted number is needed. Basically, where you've used "toFixed" function. 

 

For example:

util.printf("$%,02.2f", this.getField("BreakfastCostT1").value)

 

This returns a number prefixed with "$" and formated to use a comma as a 10^3 separator with 2 decimal places. Use it instead of ".toFixed()"

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 01, 2024 Feb 01, 2024

Use the number formatting function, util.printf().

Here's the reference  entry:

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#printf

 

 

 

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 01, 2024 Feb 01, 2024

Thom, I saw that option suggested in some other questions on the forum, but am unsure how to use in this particular instance.  What part of my script would be replaced with the util.printf(), or where would I it?  

 

I am pretty new to this and know it isn't a pretty script, but sure appreciate your help!

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 01, 2024 Feb 01, 2024

The util.printf function returns a string. So it's used anywhere a formatted number is needed. Basically, where you've used "toFixed" function. 

 

For example:

util.printf("$%,02.2f", this.getField("BreakfastCostT1").value)

 

This returns a number prefixed with "$" and formated to use a comma as a 10^3 separator with 2 decimal places. Use it instead of ".toFixed()"

 

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 02, 2024 Feb 02, 2024
LATEST

THANK YOU!!!!!  I believe this will work!  This whole project has been just beyond my reach and I've spent the last few months learning from all of you!  Thanks!

 

Now for the find/replace in my scripts!

 

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