These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Don't Show Zeroes

elcheque
Registered: Apr 7 2008
Posts: 13
Answered

Is there any way to NOT show the zeroes in a calculated field when nothing has been inputted yet?

My Product Information:
Acrobat Pro 9.4, Macintosh
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You need to write a script.

I have found that one can use the following script for the "Custom validation script" on the "Validate" tab in Acrobat:

// if value is zero replace with null string
if(event.value == 0) event.value = "";

Note the form will need to be cleared before this script will take place, as this script will not run until the field in which it is locate is calculated.

If you are writing custom calculation script, then you can add the script to your calculation.

If one uses FormCalc, then a variation of this script can be used to supress zero.

George Kaiser

elcheque
Registered: Apr 7 2008
Posts: 13
George,

Thanks for the reply. I tried it on 2 calculated fields. The one that was a simple addition worked fine. The one with a custom calculation script didn't work. It was probably because I am new to scripting and didn't incorporate it into the calculation script properly. The custom calculation script is as follows:


// Calculation script
(function () {

// Get field values as numbers
var numerator = +getField("tot_liq_assets").value;
var denominator = +getField("adj_net_worth").value;

// Perform calculation
if (denominator !== 0){
event.value = numerator / denominator;
} else {
event.value = "";
}

})();

How would you incorporate your script into this? Again. thanks for your help.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Try this:

// Calculation script
(function () {

// Get field values as numbers
var numerator = +getField("tot_liq_assets").value;
var denominator = +getField("adj_net_worth").value;
var result = numerator / denominator;

// Perform calculation
if (denominator !== 0){
event.value = (result !== 0) ? result : "";
} else {
event.value = "";
}

})();

elcheque
Registered: Apr 7 2008
Posts: 13
George,

Again, thanks for the reply. The last script didn't get rid of the zero of the calculated field. I have 2 calculated fields: "tot_liq_assets" (which is the sum of 10 other fields) and
"conc_liq_assets" which is calculated by dividing "tot_liq_assets" by an inputted field value "adj_net_worth".

The first part of the script eliminated an error message that I was getting when trying to input the 10 values (that summed to "tot_liq_assets" ) when no value for "adj_net_worth" was inputted yet.

I don't want to display zero values for "tot_liq_assets" and "conc_liq_assets" that occur before any fields are filled in.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
If your field has numeric formatting beyond the decimal point and thousand separator, you can not suppress a zero result. This means any field with a currency symbol or percentage symbol in the displayed format will display a zero result.

If you do not want to show those types of fields, then one needs to adjust the visible/hidden property of the field.

George Kaiser

elcheque
Registered: Apr 7 2008
Posts: 13
Yes, I have one of the fields formatted as a percentage, so the % sign is automatically added. What if I formatted the field as a number and added the % sign as part of the form. The calculation would the be adjusted to something like: (numerator / denominator) * 100 to yield a percentage. Would this scrip work?


/ Calculation script
(function () {

// Get field values as numbers
var numerator = +getField("tot_liq_assets").value;
var denominator = +getField("adj_net_worth").value;
var result = (numerator / denominator) * 100;

// Perform calculation
if (denominator !== 0){
event.value = (result !== 0) ? result : "";
} else {
event.value = "";
}

})();

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
What you are suggesting will make the calculation and any calculation that uses that field to behave in a non-expected manner. This can lead to errors and those errors can be very hard to track down.

I mentioned hiding the field.

You can change the 'hidden' or 'display' property of the field. Using this technique maintains the expected computation outcomes and methods. Works in Reader and Acrobat.

// Calculation script
(function () {
this.getField(event.target.name).display = display.hidden;
// Get field values as numbers
var denominator = +getField("adj_net_worth").value;// Perform calculation
if (denominator !== 0){
var numerator = +getField("tot_liq_assets").value;
event.value = numerator / denominator;
if (event.value != 0) this.getField(event.target.name).display = display.visible;
}
return
})();Or you can change the format of the field:

( function () {
// assume zero result
event.value = "";
AFNumber_Format(2, 0, 0, 0, "",false);
// Get field values as numbers
var denominator = +getField("adj_net_worth").value;
// Perform calculation
if (denominator !== 0){
var numerator = +getField("tot_liq_assets").value;
event.value = (numerator / denominator);
AFPercent_Format(2, 0, 0);
}
return;
})();

George Kaiser

elcheque
Registered: Apr 7 2008
Posts: 13
George,

Beautiful! Works like a charm. Thanks so much.
patsydelk
Registered: Dec 28 2010
Posts: 11
I have been following this topic closely as I have a similar problem. The basic validation script given on 12/15/10 at 7:49 has worked perfectly for me also as far as not showing zeros in the calculated fields, with 1 exception. There are certain lines on a form where the user needs to be able to manually enter a zero. With the basic validation script, the field will allow the user to enter a zero, but immediately overrides it so that the field is blank. For Example:

Subtract line 6 from line 5. If zero or less, enter "-0-". The validation script was entered as:

//if value is zero replace with null string
if(event.value==0)event.value="";
//if value is
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
I would not use the validation script, but use a custom calculation script for that field. Within the custom script I would test for a value in all of the fields used for the calculation, and if all have a value then I would set the value to zero and if one or more were missing a value then the null string would be the event value.

George Kaiser

patsydelk
Registered: Dec 28 2010
Posts: 11
Thanks so much-you're a great help! Another question please: I have a series of 10 fields w/calculations
on alternating fields. When I Enter a # in field 1, same # appears in fields 3, 5, & 7 as each builds off the
previous calc. Entering a new # in field 2 should cause field 3 to calculate the diff or if zero or less, I want it to be 0. Instead of changing to zero, the answer remains the same as the # entered in field 1. This is my calc:
if (this.getField("Line1").value>this.getField("Line2").value)
{
event.value=this.getField("Line1").value-this.getField("Line2").value;
}else{
event.value==0;
}
Any ideas? Thanks, Patsy
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
In what field is the calculation script located?

You may need to check the calculation order of the fields.

George Kaiser

patsydelk
Registered: Dec 28 2010
Posts: 11
Line 3
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The '==' operator is the equality operator and not the set operator, '='.

The equality operator returns either a 1 or non-zero result for a true result or returns a 0 for a false result.

George Kaiser

patsydelk
Registered: Dec 28 2010
Posts: 11
I re-worked the calc to read:
if (this.getField("Line1").value
patsydelk
Registered: Dec 28 2010
Posts: 11
Did my last post come thru in its entirety? It's not showing up
patsydelk
Registered: Dec 28 2010
Posts: 11
I have a form that cannot have any zeros in any of the fields until data is entered. The line reads "Subtract Line 2 from Line 1. If zero or less, enter "0" ". The calculation I have is working perfectly, with 1 exception: (1) if Line 1 is > Line 2, it subtracts perfectly; (2) if Line 1 is < Line 2, it inserts a 0; (3) it has no zeros in any of the fields until data is actually entered. The problem is if Line 1 & 2 are =, it won't automatically return a 0. My script is as follows:
(function(){
event.value="";
if(this.getField("EstimateDed").value>this.getField("FileStatus").value)
{
event.value=this.getField("EstimateDed").value-this.getField("FileStatus").value;
}
if(this.getField("EstimateDed").value
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
It is pretty hard to figure anything out with a partial script.

You need to add a test to see if line 1 or line 2 has a non-blank value and if one mets that test, then the result of the calculation should be displayed.

For line 3 is the difference between line 1 and line 2, the custom calculation script for line 3 could be:

//establish values for field names for value 1 and value 2
var cLine1 = 'line 1'; // field name 1
var cLine2 = 'line 2'; // field name 2
// get values for line 1 and line 2
var nValue1 = this.getField(cLine1).value;
var nValue2 = this.getField(cLine2).value;
// by default null the value of the result field
event.value = "";
// test for a value in line 1 or line 2
if(nValue1.toString() != "" | nValue2.toString() != "") {
// we have a value - compute result
event.value = nValue1 - nValue2;
}

You only need to change the field names. This also shows that it is possible to write generalized code and since JavaScript supports functions, it is possible to write a callable function to perform this calculation with just providing the field names or values.

George Kaiser

patsydelk
Registered: Dec 28 2010
Posts: 11
I must be entering my posts incorrectly as half of my messages are cut off. Help?
graphicsguy
Registered: Jan 13 2011
Posts: 2
I just learned how to post a new forum topic. Please disregard this post.

Thank you.

patsydelk
Registered: Dec 28 2010
Posts: 11
George, you're a genius! Works perfectly! Thanks, Patsy
patsydelk
Registered: Dec 28 2010
Posts: 11
I have an additional problem related to this post. It reads: Subtract Line 19 from Line 18. Enter the result, including negative amounts. If your employer cannot accommodate negative balances, enter zero here." The script you provided on 1-18-2011 worked perfectly, with one exception. I need an additional script so that, if a user receives a negative difference, the user can override that with a zero. I'm hoping you can help, George. Thanks, Patsy