Skip to main content
Participant
March 12, 2025
Question

Add only negative numbers in a set of data

  • March 12, 2025
  • 1 reply
  • 377 views

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?

1 reply

Nesa Nurani
Community Expert
Community Expert
March 12, 2025

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;

 

Participant
March 26, 2025

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.

Nesa Nurani
Community Expert
Community Expert
March 27, 2025

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