# Calculation to show 0 when 0 is entered otherwise will be blank when SPECIFIC fields are empty.

Community Beginner ,
Mar 17, 2020 Mar 17, 2020

Copied

Hello All.

Here is my problem.

Field "A" is populated from a previous calculation.
Next calulation is A x B x C = D.
If B and C is 0 (zero), then D will diplay 0 (zero)

If B & C are left blank, then D will display an empty field

Field A & D are read only

I've a can't seem to finds a script that applies to the above.

I would appreciate any help.

Thanks.

TOPICS
Acrobat SDK and JavaScript, Comment review and collaborate PDF, Create PDFs, General troubleshooting, How to, PDF forms

Views

636

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Adobe Community Professional , Mar 17, 2020 Mar 17, 2020
In the custom calculation script for D, you could do something like: // Custom calculate script (function () { // Get the input field values, as strings var sA = getField("A").valueAsString; var sB = getField("B").valueAsString; var sC = getField("C").valueAsString; if (sB && sC) { // If both of these input fields are not empty... // If both of these field values are zero when converted to a number... if (+sB === 0 && +sC === 0) { event.value...

Likes

Community Beginner , Mar 17, 2020 Mar 17, 2020
Code works. I replaced with incorrect field names. Thanks George_Johnson-9fk31b

Likes

7 Replies 7
Mar 17, 2020 Mar 17, 2020

Copied

In the custom calculation script for D, you could do something like:

``````// Custom calculate script
(function () {

// Get the input field values, as strings
var sA = getField("A").valueAsString;
var sB = getField("B").valueAsString;
var sC = getField("C").valueAsString;

if (sB && sC) {  // If both of these input fields are not empty...

// If both of these field values are zero when converted to a number...
if (+sB === 0 && +sC === 0) {
event.value = 0;  // Set this field value to zero
return;
} else {
event.value = sA * sB * sC;  // Set this field value to this product
return;
}

} else {
// Blank this field since B and C inputs are blank
event.value = "";
return;
}

})();``````

You'd have to change the field names in the getField statements to the actual field names. Be sure to set the field calculation order to what makes sense.

Likes

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 Beginner ,
Mar 17, 2020 Mar 17, 2020

Copied

Sorry.

I get what the script is trying to do but what should I be copying to paste in. I'm getting errors.

The names of my fields are
A=g

B=gg
C=yy

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 17, 2020 Mar 17, 2020

Copied

It would be helpful to know what the error messages you're getting actually say...

Likes

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 Beginner ,
Mar 17, 2020 Mar 17, 2020

Copied

The script its what

```// Custom calculate script
(function () {

// Get the input field values, as strings
var sA = getField("A").valueAsString;
var sB = getField("B").valueAsString;
var sC = getField("C").valueAsString;

if (sB && sC) {  // If both of these input fields are not empty...

// If both of these field values are zero when converted to a number...
if (+sB === 0 && +sC === 0) {
event.value = 0;  // Set this field value to zero
return;
} else {
event.value = sA * sB * sC;  // Set this field value to this product
return;
}

} else {
// Blank this field since B and C inputs are blank
event.value = "";
return;
}

})()```

Likes

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 Beginner ,
Mar 17, 2020 Mar 17, 2020

Copied

LATEST

Code works. I replaced with incorrect field names.

Thanks George_Johnson-9fk31b

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 17, 2020 Mar 17, 2020

Copied

This condition is not necessary:

If B and C is 0 (zero), then D will display 0 (zero)

If either of those fields is zero then the result of multiplying them by any other value will be zero, no matter what.

Likes

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 Beginner ,
Mar 17, 2020 Mar 17, 2020

Copied