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

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

Community Beginner ,
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

277

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;``````

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 & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

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

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;``````

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

Copied

LATEST

This is perfect! Thank you!