Skip to main content
Participating Frequently
January 10, 2020
Question

Percent and decimal script

  • January 10, 2020
  • 5 replies
  • 2565 views

How do I use the below script on more than 1 field as current it gives an error on other fields

if (event.value != "") {
event.value = util.printf("%0.2f",event.value)+"%";
}

 

This topic has been closed for replies.

5 replies

Participating Frequently
January 24, 2020

I updated my program to show decimal and % sign on my calculation

var x = Number(event.value).toFixed(2);
event.value = x.toString()+ " %"
if(event.value == 0 || event.value == "0.00 %") {
event.value = "";}
Thom Parker
Community Expert
Community Expert
January 24, 2020

Is it working now?

You know, that after line #2 that "event.value" will never be 0. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
January 24, 2020

Yes it is working. Sometimes the form is printed without filling the fields so we have to remove the 0

Inspiring
January 18, 2020

Do you have any calculations that are using division?

Thom Parker
Community Expert
Community Expert
January 15, 2020

Try this, remove the format script so you can see what the "value" is that is causeing trouble. Then you can add code to test for that value. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
January 17, 2020

I removed the string "%" and there is no error but once the string is removed there is no need for the script as the number format category displays the decimal. The script was created to have both the decimal and % sign out put. Another way would be using the percentage category and write a script to divide the value by 100.

Thom Parker
Community Expert
Community Expert
January 18, 2020

So the idea is to find out what value is causing the problem with util.printf. Did you find out this infomation?

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Legend
January 10, 2020

By design, util.printf("%0.2f",x) will write 1.#R where x is not a number. 

Participating Frequently
January 13, 2020

Please simplify your response.

How do I my code to avoid this from happening

Legend
January 13, 2020

Do not format data that is not a number. Check your input. 

Don't ask me to write your script, please.

Bernd Alheit
Community Expert
Community Expert
January 10, 2020

Where does you use this script?

Participating Frequently
January 13, 2020

Adobe Accrobat DC standard

Bernd Alheit
Community Expert
Community Expert
January 15, 2020

Does you use the code as calculation script?