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

Copy link to clipboard

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

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

correct answers 2 Correct Answers

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

Translate

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

Likes

Translate

Translate
Adobe Community Professional ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

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

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

Copy link to clipboard

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

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
Adobe Community Professional ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

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

Also, post your full 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 ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

The script its what George_Johnson-9fk31b left.


 

// 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

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

Copy link to clipboard

Copied

LATEST

Code works. I replaced with incorrect field names.

Thanks George_Johnson-9fk31b

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
Adobe Community Professional ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

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

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

Copy link to clipboard

Copied

It's not about the math. It's about what displays.

B & C are populated by user. BUT if they enter a 0 (zero). The resulting field will show 0 (zero). IF they leave it blank then the resulting field will be blank.

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