• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
• Re: Custom Calculation Script: Sum, only add posit...

# Custom Calculation Script: Sum, only add positive values (Exclude negative values)

New Here ,
Sep 07, 2021 Sep 07, 2021

Copied

I am making a form to quote out multiple products, where the customer receives a rebate.  The product is taxable, the rebate does not effect the taxable value of the quote.  The way I am formatting it, Product & Rebates are added in the body of the quote.

For example, if a product is \$100, the rebate is \$50 (written as -75), and the Sales Tax is 7%, I need to subtotal to read as \$100, and not \$25, since the sum adds positive and negative number together.

How can I write a custom script where all positive fields are added together, but if the value of a field is negative, the calulation treats it as a value of 0, rather than it's negative value?

Views

78

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 Expert , Sep 08, 2021 Sep 08, 2021

You can use this code as the custom calculation script of your field (adjust the first line, of course):

``````var fieldsToAdd = ["Text1", "Text2", "Text3"]; // Replace with actual field names
var total = 0;
for (var i in fieldsToAdd) {
var v = Number(f.valueAsString);
if (v>0) total+=v;
}
event.value = total;``````

Likes

4 Replies 4
Community Expert ,
Sep 07, 2021 Sep 07, 2021

Copied

Which Adobe app or service are you using?

Nancy O'Shea, Product User & Community Expert
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

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
New Here ,
Sep 07, 2021 Sep 07, 2021

Copied

I am using Adobe Acrobat Pro. I have used many custom calulations before with great success, but this one illudes me.

Moving from Using the Community (which is about the forums) to the correct forum... Mod

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 Expert ,
Sep 08, 2021 Sep 08, 2021

Copied

You can use this code as the custom calculation script of your field (adjust the first line, of course):

``````var fieldsToAdd = ["Text1", "Text2", "Text3"]; // Replace with actual field names
var total = 0;
for (var i in fieldsToAdd) {
var v = Number(f.valueAsString);
if (v>0) total+=v;
}
event.value = total;``````

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
New Here ,
Sep 08, 2021 Sep 08, 2021

Copied

LATEST

This is perfect! Thank you!

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
Resources