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?
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.
Copy link to clipboard
Copied
No Error; but format is now wrong but I can fix that in code
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...
Copy link to clipboard
Copied
What is the error message?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Instead of setting the field value to blank, set it to 0. The Percentage formatting expects a number
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?
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):"";
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
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.
Copy link to clipboard
Copied
Thanks. I thought I had seen it before. It's been many years since I used C, C++, or C#.
Found the reference