public static BigDecimal avg(final Iterable<? extends Number> target) {
        Validate.notNull(target, "Cannot aggregate on null");
        Validate.containsNoNulls(target, "Cannot aggregate on array containing nulls");
        BigDecimal total = BigDecimal.ZERO;
        int size = 0;
        for (final Number element : target) {
            total = total.add(toBigDecimal(element));
            size++;
        }
        if (size == 0) {
            return null;
        }
        final BigDecimal divisor = BigDecimal.valueOf(size);
        try {
            return total.divide(divisor);
        } catch (final ArithmeticException e) {
            // We will get an arithmetic exception if: 1. Divisor is zero, which is impossible; or 2. Division
            // returns a number with a non-terminating decimal expansion. In the latter case, we will set the
            // scale manually.
            return total.divide(divisor, Math.max(total.scale(), 10), RoundingMode.HALF_UP);
        }
    }
public static BigDecimal avg ( final Iterable < ? extends Number > target ) { Validate . notNull ( target , "Cannot aggregate on null" ) ; Validate . containsNoNulls ( target , "Cannot aggregate on array containing nulls" ) ; BigDecimal total = BigDecimal . ZERO ; int size = 0 ; for ( final Number element : target ) { total = total . add ( toBigDecimal ( element ) ) ; size ++ ; } if ( size == 0 ) { return null ; } final BigDecimal divisor = BigDecimal . valueOf ( size ) ; try { return total . divide ( divisor ) ; } catch ( final ArithmeticException e ) { return total . divide ( divisor , Math . max ( total . scale ( ) , 10 ) , RoundingMode . HALF_UP ) ; } }
<p>
Returns the average of all the numbers contained in the provided
iterable (e.g. a collection).
</p>

@param target the iterable containing the number objects
@return the average, as a BigDecimal