Skip to main content
Participant
March 11, 2020
Answered

Formatting a calulated field in javascript

  • March 11, 2020
  • 1 reply
  • 2129 views

I have a calculation that needs to return a currency value ($ symbol & two decimal places) if a field (A) used in the calculation is filled in (ie: showing a number value) then the Result field must show $x,xxx.xx.

(Currency + comma separator + two decimal places)

 

My calculation is set to be blank when field (A) is blank. I can't set the format for my result field to currency as it returns an error when Field (A) is changed from a value to blank.

 

What is the javascript to set this up and is it entered into the custom format script block?

Thanks

 

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

    Hi Thom

    Thanks, but that doesn't work either - still shows the $0.00


    Opps should have not forced it to a string

     

    event.value = ((event.value==0) || isNaN(event.value))?"": util.printf("$%,0.2f",event.value);

     

    Just to be sure though. Before making this update. Remove all formatting from the field. 

    What appears in the result field when A is blank?  The script will only show a blank if the actual calculated field value is either a 0 or something that is not a number. 

     

     

     

    1 reply

    Thom Parker
    Community Expert
    Community Expert
    March 11, 2020

    Put this code in the custom format script.

     

    event.value = ((event.valueAsString=="") || isNaN(event.value))?"": util.printf("$%,0.2f",event.value);

     

    Here's an old article on formatting 

    https://acrobatusers.com/tutorials/formatting_text_fields/

    Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
    Dot-WynneAuthor
    Participant
    March 11, 2020

    Thanks - however, this shows $0.00 when Field (A) value is blank and I want it to be a blank field.