Is there an average method for apex math

Unfortunately the standard math methods only include simpler operations (i.e. those that work on a single, or two values), so it looks as though you'll have to roll your own method.

Of course the number of script statements executed will be proportional to the length of the list, so of the lists are ever of a fixed size it could be worth using a macro to generate the addition part for you:

Int sum = i[0] + i[1] + ... i[n];

Doing so would only count for one statement, but you'll only need this if governor limits are of concern which is often not a worry.

If govenor limits aren't an issue you could create a function along these lines:

Integer[] myInts = new Integer[]{1, 2, 3, 4, 5, 6, 7};
Integer total = 0;
Double dAvg;

for (Integer i : myInts) {
    total += i;
}

dAvg = Double.valueOf(total) / myInts.size();

return dAvg;

Tags:

Math

Apex