Java BigDecimal average(final BigDecimal... numbers)

Here you can find the source of average(final BigDecimal... numbers)

Description

average

License

Apache License

Declaration

public static BigDecimal average(final BigDecimal... numbers) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.stream.IntStream;

public class Main {
    public static BigDecimal average(final BigDecimal... numbers) {
        return IntStream.range(0, numbers.length).mapToObj(i -> numbers[i]).reduce((sum, next) -> sum.add(next))
                .map(sum -> sum.divide(BigDecimal.valueOf(numbers.length))).orElse(BigDecimal.ZERO);
    }/*ww  w . j a  v  a  2s  . c  o m*/

    public static BigDecimal divide(final BigDecimal amount, final BigDecimal divideBy, final int scale) {
        return amount.setScale(scale, RoundingMode.HALF_EVEN).divide(divideBy, RoundingMode.HALF_EVEN);
    }
}

Related

  1. adjustCurrencyDecimalPoint(final BigDecimal value, final int decimal)
  2. areEqual(BigDecimal aThis, BigDecimal aThat)
  3. areEqual(BigDecimal first, BigDecimal second)
  4. asArray(final BigDecimal... elements)
  5. average(BigDecimal... values)
  6. bigDecimalListToArray(List list)
  7. bigDecimalObjectValue(Object input)
  8. bigDecimalParse(String value)
  9. bigDecimalPrint(BigDecimal value)