Skip to main content
Known Participant
Nov 6, 2019 4:24 PM UTC
Question

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

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 is closed to new replies. Start a new post to keep the conversation going.

11 replies

Bernd Alheit
Community Expert
Community Expert
Nov 6, 2019 4:44 PM UTC

What is the error message?

Translate
kct3937Author
Known Participant
Nov 6, 2019 5:16 PM UTC

 

Translate
Thom Parker
Community Expert
Community Expert
Nov 6, 2019 5:35 PM UTC

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

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
Nov 6, 2019 4:43 PM UTC

Do you have other calculations within the form?

 

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

Translate
kct3937Author
Known Participant
Nov 6, 2019 5:18 PM UTC

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

Translate
kct3937Author
Known Participant
Nov 6, 2019 6:39 PM UTC

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 !== 0) event.value = (v1 / v2) * 100 + '%';
else event.value = '';
Translate