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

Formatting a calulated field in javascript

New Here ,
Mar 10, 2020 Mar 10, 2020

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

 

2.0K
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 ,
Mar 11, 2020 Mar 11, 2020
LATEST

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. 

 

 

 

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 ,
Mar 10, 2020 Mar 10, 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 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
New Here ,
Mar 10, 2020 Mar 10, 2020

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

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
New Here ,
Mar 10, 2020 Mar 10, 2020

Screen Shot 2020-03-11 at 1.41.29 PM.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 ,
Mar 11, 2020 Mar 11, 2020

The problem is that the calculation is setting the field value to zero.  Here's an easy solution.

 

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

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
New Here ,
Mar 11, 2020 Mar 11, 2020

Hi Thom

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

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 ,
Mar 11, 2020 Mar 11, 2020
LATEST

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. 

 

 

 

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