cn.acyou.leo.framework.util.MathUtil Maven / Gradle / Ivy
package cn.acyou.leo.framework.util;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collection;
import java.util.OptionalDouble;
/**
* @author youfang
* @version [1.0.0, 2020/4/3]
**/
public class MathUtil {
public static final BigDecimal HUNDRED = new BigDecimal("100");
public static final BigDecimal PERCENT = new BigDecimal("0.01");
/**
* 计算百分之xx
*
* calculationPercent(2, 5) = 40
* calculationPercent(3, 5) = 60
*
*
* @param molecule 分子
* @param denominator 分母
* @return xx%
*/
public static Integer calculationPercent(Number molecule, Number denominator) {
if (denominator.longValue() == 0){
return 0;
}
BigDecimal moleculeDecimal = new BigDecimal(molecule.longValue());
BigDecimal denominatorDecimal = new BigDecimal(denominator.longValue());
BigDecimal divide = moleculeDecimal.divide(denominatorDecimal, 2, RoundingMode.HALF_UP);
return divide.multiply(HUNDRED).intValue();
}
/**
* 计算平均数
*
* @param sourceNumbers 数据源
* @return 平均数
*/
public static Double averageDouble(Collection