Here you can find the source of calculateArithmeticMean(BigDecimal[] values)
Parameter | Description |
---|---|
values | a parameter |
private static BigDecimal calculateArithmeticMean(BigDecimal[] values)
//package com.java2s; //License from project: Apache License import java.math.BigDecimal; public class Main { /**/*from www . ja va2 s. com*/ * The scale for calculations with BigDecimal. */ private static final int DECIMAL_SCALE = 10; /** * Get the arithmetic mean for a set of values. Using BigDecimal for exact * results. * * @param values * @return The arithmetic mean. */ private static BigDecimal calculateArithmeticMean(BigDecimal[] values) { if (values.length == 1) { return values[0].setScale(DECIMAL_SCALE, BigDecimal.ROUND_HALF_UP); } BigDecimal arithmeticMean = BigDecimal.valueOf(0d); for (BigDecimal value : values) { arithmeticMean = arithmeticMean.add(value); } arithmeticMean = arithmeticMean.divide(BigDecimal.valueOf(values.length), DECIMAL_SCALE, BigDecimal.ROUND_HALF_UP); return arithmeticMean; } /** * Get the arithmetic mean for a set of values. Using BigDecimal for exact * results. * * @param values * @return The arithmetic mean. */ public static double calculateArithmeticMean(double[] values) { BigDecimal[] valuesBigD = new BigDecimal[values.length]; for (int i = 0; i < values.length; i++) { valuesBigD[i] = BigDecimal.valueOf(values[i]); } return calculateArithmeticMean(valuesBigD).doubleValue(); } }