Skip to main content
Participant
Mar 12, 2025 5:34 PM UTC
Question

Add only negative numbers in a set of data

I have an expense report that has fields for "New Charges" and "Credits". Each of these comes from a set of data listed as "Amount.x". (Think a long row of charges named Amount.1, Amount.2... so on).

 

I want "New Charges" to be the sum of numbers greater than or equal to 0, and "Credits" to be the sum of anything less than 0.  How do I write a script for these?

3 replies

Nesa Nurani
Community Expert
Community Expert
Mar 12, 2025 6:53 PM UTC

Try this, just replace 4 with the number of "Amount" fields you have:

var charges = 0;
var credits = 0;

for(var i=1; i<=4; i++){
 var f = Number(this.getField("Amount."+i).valueAsString);
  if(f > 0)
   charges += f;
  if(f < 0)
   credits += f;}

this.getField("New Charges").value = charges;
this.getField("Credits").value = credits;

 

Translate
Participant
Mar 26, 2025 7:14 PM UTC

Nope, not the answer. I attached a picture if that helps. 

 

The itemized total is correct, but "New Charges" should read 5.00 and "Credits" should read 2.00.

I also have a setting turned on that that makes negative numbers display in (). If that makes a difference.

Translate
Nesa Nurani
Community Expert
Community Expert
Mar 27, 2025 5:43 AM UTC

Check console for errors (CTRL+J) and also make sure your fields are named correctly.
If you can post your file.

Translate