Java BigDecimal mean(List numbers, MathContext context)

Here you can find the source of mean(List numbers, MathContext context)

Description

Returns the mean number in the numbers list.

License

Open Source License

Parameter

Parameter Description
numbers the numbers to calculate the mean.
context the MathContext.

Return

the mean of the numbers.

Declaration

public static BigDecimal mean(List<BigDecimal> numbers, MathContext context) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.math.BigDecimal;

import java.math.MathContext;

import java.util.List;

public class Main {
    /**/*  w  w  w.  j  a v a2  s .  c o  m*/
    * Returns the mean number in the numbers list.
    *
    * @param numbers the numbers to calculate the mean.
    * @param context the MathContext.
    * @return the mean of the numbers.
    */
    public static BigDecimal mean(List<BigDecimal> numbers, MathContext context) {
        BigDecimal sum = sum(numbers);
        return sum.divide(new BigDecimal(numbers.size()), context);
    }

    /**
    * Returns the sum number in the numbers list.
    *
    * @param numbers the numbers to calculate the sum.
    * @return the sum of the numbers.
    */
    public static BigDecimal sum(List<BigDecimal> numbers) {
        BigDecimal sum = new BigDecimal(0);
        for (BigDecimal bigDecimal : numbers) {
            sum = sum.add(bigDecimal);
        }
        return sum;
    }
}

Related

  1. invert(BigDecimal d)
  2. joinBigDecimals(List list)
  3. jsonNumberToBigDecimal(final JsonNumber n, final int defaultValue)
  4. limitScale(BigDecimal value, int scale)
  5. matchScale(BigDecimal[] val)
  6. median(final BigDecimal[] bigDecimalNumbers)
  7. milliToCent(BigDecimal val)
  8. milliToDollar(BigDecimal val)
  9. mod(long res, BigDecimal value)