Java BigDecimal Round average(int precision, RoundingMode roundingMode, BigDecimal... decimals)

Here you can find the source of average(int precision, RoundingMode roundingMode, BigDecimal... decimals)

Description

Returns the average or mean from the given list of decimal numbers.

License

Open Source License

Parameter

Parameter Description
precision The number of decimal places to round to.
roundingMode The rounding algorithm to be used.
decimals A list of decimal numbers.

Return

The average or mean value of the given list of values.

Declaration

public static BigDecimal average(int precision, RoundingMode roundingMode, BigDecimal... decimals) 

Method Source Code


//package com.java2s;
/*// w ww.  jav a  2 s.co m
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.math.BigDecimal;

import java.math.RoundingMode;

public class Main {
    /**
     * The default rounding mode used by the methods in this class.
     */
    public static RoundingMode DEFAULT_ROUNDING_MODE = RoundingMode.HALF_UP;
    /**
     * The default decimal precision used by the methods in this class.
     */
    public static int DEFAULT_DECIMAL_PRECISION = 0;

    /**
     * Returns the average or mean from the given list of decimal numbers.
     *
     * @param precision    The number of decimal places to round to.
     * @param roundingMode The rounding algorithm to be used.
     * @param decimals     A list of decimal numbers.
     * @return The average or mean value of the given list of values.
     */
    public static BigDecimal average(int precision, RoundingMode roundingMode, BigDecimal... decimals) {
        BigDecimal result = null;

        if (decimals != null) {
            int length = 0;
            for (BigDecimal decimal : decimals) {
                if (decimal != null) {
                    if (result == null) {
                        result = decimal;
                    } else {
                        result = result.add(decimal);
                    }
                    length++;
                }
            }
            if (result != null) {
                result = divide(result, new BigDecimal(length), precision, roundingMode);
            }
        }

        return result;
    }

    /**
     * Returns the average or mean from the given list of decimal numbers.
     *
     * @param precision    The number of decimal places to round to.
     * @param roundingMode The rounding algorithm to be used.
     * @param decimals     A list of decimal numbers.
     * @return The average or mean value of the given list of values.
     */
    public static BigDecimal average(String precision, String roundingMode, BigDecimal... decimals) {
        return average(normalizePrecision(precision, decimals), normalizeRoundingMode(roundingMode), decimals);
    }

    /**
     * Returns the average or mean from the given list of decimal numbers.
     *
     * @param decimals A list of decimal numbers.
     * @return The average or mean value of the given list of values.
     */
    public static BigDecimal average(BigDecimal... decimals) {
        return average(null, null, decimals);
    }

    /**
     * Returns the sum of all the given decimals.
     *
     * @param operands The decimal numbers to be summed.
     * @return The sum of all the given decimal numbers.
     */
    public static BigDecimal add(BigDecimal... operands) {
        BigDecimal result = null;

        if (operands != null) {
            for (BigDecimal operand : operands) {
                if (operand != null) {
                    if (result == null) {
                        result = operand;
                    } else {
                        result = result.add(operand);
                    }
                }
            }
        }

        return result;
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend The decimal to be divided.
     * @param divisor  The decimal to divide by.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor) {
        return divide(dividend, divisor, (RoundingMode) null);
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend  The decimal to be divided.
     * @param divisor   The decimal to divide by.
     * @param precision The number of decimal places preserved in the result.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int precision) {
        return divide(dividend, divisor, precision, null);
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend     The decimal to be divided.
     * @param divisor      The decimal to divide by.
     * @param roundingMode The rounding algorithm to use when rounding the result.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, RoundingMode roundingMode) {
        return divide(dividend, divisor, getMaxPrecision(dividend, divisor), roundingMode);
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend     The decimal to be divided.
     * @param divisor      The decimal to divide by.
     * @param precision    The number of decimal places preserved in the result.
     * @param roundingMode The rounding algorithm to use when rounding the result.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int precision,
            RoundingMode roundingMode) {
        if (roundingMode == null)
            roundingMode = DEFAULT_ROUNDING_MODE;

        BigDecimal result = null;

        if (dividend != null && divisor != null) {
            result = dividend.divide(divisor, precision, roundingMode);
        }

        return result;
    }

    /**
     * Divides the given dividend by the divisor.
     *
     * @param dividend     The decimal to be divided.
     * @param divisor      The decimal to divide by.
     * @param precision    The number of decimal places preserved in the result.
     * @param roundingMode The rounding algorithm to use when rounding the result.
     * @return The result of dividing the dividend by the divisor.
     */
    public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, String precision,
            String roundingMode) {
        return divide(dividend, divisor, normalizePrecision(precision, dividend, divisor),
                normalizeRoundingMode(roundingMode));
    }

    /**
     * Returns the specified precision unless it is null, in which case the maximum precision from the list of decimals
     * is returned.
     *
     * @param precision Optional precision to be returned.
     * @param decimals  If precision not specified, the maximum precision from this list of decimals is returned.
     * @return The resolved precision to be used by the caller.
     */
    private static int normalizePrecision(String precision, BigDecimal... decimals) {
        int result;
        if (precision != null) {
            result = Integer.parseInt(precision);
        } else {
            result = getMaxPrecision(decimals);
        }
        return result;
    }

    /**
     * Returns the given rounding mode if specified, or the default rounding mode.
     *
     * @param roundingMode An optional rounding algorithm name.
     * @return The rounding algorithm to be used by the caller.
     */
    private static RoundingMode normalizeRoundingMode(String roundingMode) {
        return roundingMode == null ? DEFAULT_ROUNDING_MODE : RoundingMode.valueOf(roundingMode);
    }

    /**
     * Returns the maximum precision used by the given list of decimals.
     *
     * @param decimals A list of decimals to calculate the maximum precision of.
     * @return The maximum precision used by the given list of decimals.
     */
    private static int getMaxPrecision(BigDecimal... decimals) {
        int precision = DEFAULT_DECIMAL_PRECISION;
        if (decimals != null) {
            for (BigDecimal decimal : decimals) {
                if (decimal != null && decimal.scale() > precision)
                    precision = decimal.scale();
            }
        }
        return precision;
    }
}

Related

  1. decimalRound(BigDecimal number, int numDigits, RoundingMode rounding)
  2. getEfficientRound(BigDecimal val, int effectiveDigit)
  3. getRoundedAmt(BigDecimal amtBeforeRnd)
  4. getRoundedBigDecimal(double value)