Skip to main content
Participating Frequently
February 1, 2024
Answered

Currency format in custom calculation script

  • February 1, 2024
  • 1 reply
  • 1504 views

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:

 

 

Any assistance would be helpful!

THANKS!

This topic has been closed for replies.
Correct answer Thom Parker

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

 

1 reply

Thom Parker
Community Expert
Community Expert
February 1, 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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
February 1, 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!

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
February 2, 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 PDFScriptingUse the Acrobat JavaScript Reference early and often