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

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

Community Beginner ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript

Views

249

Likes

Translate

Translate

Report

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
LEGEND ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

Do you have other calculations within the form?

 

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

Likes

Translate

Translate

Report

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 Beginner ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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 Beginner ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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 = '';

Likes

Translate

Translate

Report

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 ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

What is the error message?

Likes

Translate

Translate

Report

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 Beginner ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

Screen Shot 2019-11-06 at 9.14.15 AM.png

 

Likes

Translate

Translate

Report

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 ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

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

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

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 Beginner ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

Thanks Thom, that what I thought.  However, as with other programming, it's the boundary conditions that get you...especially zeros.

I tried to get around that becuase I don't want the 0 to show in the calc field since the two entry fields can be left blank/not used.

I also tried using v1!==0 && v2 !== 0.

 

Could I hide the calculated field if the two inputs are blank and just not do the calulation?

Likes

Translate

Translate

Report

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 ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

You have to either go with how the built-in formatting works, or write your own formatting. 

The percentage is pretty simple. 

 

For the calculation, test for a zero dividend before doing the calculation. 

Then enter this into the custom format script:

event.value = (event.value!=0) && (event.value!="")?util.printf("%0.f%",event.value/100):"";

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

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 Beginner ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

OK so I see the ternary function.  I didn't know whether to use it in this forum and risk confusing some users.
I take it that you are saying it's OK to test the value of the calculation before writing it to the screen; where I was testing the inputs.

 

I have not seen the expression '%0.f%' before.  I'm not sure what the f stands for. My guess would be 'float' or 'format' as I have seen it that way in other languages.

Can you please explain?

 

thanks

 

Ken T

 

Likes

Translate

Translate

Report

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 ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

I would suggest you lookup the "util.printf()" function in the Acrobat JavaScript reference.  The formatting string for this function is taken directly from C. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Translate

Translate

Report

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 Beginner ,
Nov 06, 2019 Nov 06, 2019

Copy link to clipboard

Copied

LATEST

Thanks.  I thought I had seen it before.  It's been many years since I used C, C++, or C#.

 

Found the reference

Likes

Translate

Translate

Report

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