Returns the mean number in the BigDecimal list. - Java java.math

Java examples for java.math:BigDecimal Calculation

Description

Returns the mean number in the BigDecimal list.

Demo Code


//package com.java2s;
import java.math.BigDecimal;

import java.math.MathContext;

import java.util.List;

public class Main {
    /**/* w  w  w.  j a  va2s  . co 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 Tutorials