Skip to main content
Known Participant
November 6, 2019
Question

Checking for form fields for zero value(s) before performing a calculation; such as percentage

  • November 6, 2019
  • 2 replies
  • 1285 views

I have a calculated field that is formatted as a percentage and references two other fields.  I am checking for zeros before doing the calculation.  The answer is correct but I get an error about the formatting of the answer when zeros are found.

I am using the following code which I got from an answer by try67 ...

 

var v1 = Number(this.getField('testWt').valueAsString);

var v2 = Number(this.getField('ratedCap').valueAsString);

if (v2 == 0 || v1 == 0) event.value = '';
else event.value = v1 / v2;

 

How can I stop the error message as I want the answer as a percentage?

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
November 6, 2019

What is the error message?

kct3937Author
Known Participant
November 6, 2019

 

Thom Parker
Community Expert
Community Expert
November 6, 2019

Instead of setting the field value to blank, set it to 0. The Percentage formatting expects a number

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
November 6, 2019

Do you have other calculations within the form?

 

Try setting the format of the field to "None" and observe what happens.

kct3937Author
Known Participant
November 6, 2019

No Error; but format is now wrong but I can fix that in code

kct3937Author
Known Participant
November 6, 2019

There are no other calculated fields in the form at this time.

Setting all the involved fields to format:none works to a degree.  The fields calculate properly and display nothing if nothing is entered. However any calculation now includes too many decimal places where I would like it to be 2 maximum.

It looks like I need to round the result of the calculation but Round(v1/v2, 2) doesn't seem to work.

 

Since I changed the format I am concatenating the result to the percentage sign.

 

 

Here is the code...

var v1 = Number(this.getField('testWt').valueAsString);

var v2 = Number(this.getField('ratedCap').valueAsString);

if (v2 !== 0 && v1 !== 0event.value = (v1 / v2* 100 + '%';
else event.value = '';